Discussions

EJB design:  How to properly handle Exceptions?

  1.  How to properly handle Exceptions? (4 messages)

    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

    Threaded Messages (4)

  2.  How to properly handle Exceptions?[ Go to top ]

    Best practices in EJB exception handling:
    http://www-106.ibm.com/developerworks/library/j-ejbexcept.html

  3. Josh Bloch's book "Effective Java" has some wonderful exception handling advice.

  4. As far as i know its not good to display original exception. Here having keep in mind the end user, proper message should be passed. so better use user defined exceptions. First catch the original sql exception according to that rethrow the user defined exception..catch it in the client and display the exception description to the end user.

    thanking you
  5.  How to properly handle Exceptions?[ Go to top ]

    SQL exceptions should not happen at all. YOur application should guard against them. If they do happen, you are better off displaying some kind of error to the user that tells them the issue has been logged and then to use Log4J to sned the log of the error to a JMS source which will can then be routed to the appropriate recipient.