2 Replies Latest reply on Sep 9, 2002 8:26 PM by Joel Vogt

    How to properly handle Exceptions?

    L.G. Newbie

      Client would check if exception exists in HashMap and display it to user. If there is no Exception I would put the value(s) in HashMap and client would use it.
      ====================================================
      public class MySessionBean implements SessionBean{
      ...
      public HashMap myMethod(){
      HashMap retValue = new HashMap();
      String someValue;
      ...
      try{
      MyEntityBean myEntityBean = ...;
      someValue = myEntityBean.doSomething();
      }catch (Exception e){
      retValue.put("Exception", e.getMessage());
      return retValue;
      }
      retValue.put("someValue", someValue);
      return retValue;
      }
      }
      =======================================================
      Is it good/bad solution?

      TIA

        • 1. Re: How to properly handle Exceptions?
          L.G. Newbie

          Sorry, just didn't copy all the text...
          So,

          I'm trying to find out how to properly handle Exceptions.
          For example if entity bean throws SQL exception because there is missing value for non nullable column I want to show in JSP errorPage original database error text. Unfortunally it will be something else.
          Is it feasible to catch first exception in session facade, extract error message, and instead of re-throwing Exception put message in HashMap and return it to client.
          Client would check if exception exists in HashMap and display it to user. If there is no Exception I would put the value(s) in HashMap and client would use it.
          ====================================================
          public class MySessionBean implements SessionBean{
          ...
          public HashMap myMethod(){
          HashMap retValue = new HashMap();
          String someValue;
          ...
          try{
          MyEntityBean myEntityBean = ...;
          someValue = myEntityBean.doSomething();
          }catch (Exception e){
          retValue.put("Exception", e.getMessage());
          return retValue;
          }
          retValue.put("someValue", someValue);
          return retValue;
          }
          }
          =======================================================
          Is it good/bad solution?

          TIA

          • 2. Re: How to properly handle Exceptions?
            Joel Vogt Master

            The problem with this solution is that is some cases you want your session bean to throw an exception back up. For example you might use an ejb exception to trigger a transaction rollback.
            What is nicer is to have a layer below the session bean that will catch your rethrown exception and then work out how to best display it in your presentation tier.

            For example, I often use a struts action that accesses a session bean to handle events. If an event fails an event exception is thrown back to the action which then extracts the message, puts it in the session and forwards to an error jsp to display it.