EJB programming & troubleshooting: nested Exception Handling

  1. nested Exception Handling (1 messages)

    I am trying to handle nested SQLException in my Application but I am not able to verify that exception occured is due to SQLError, As I am getting EJBException from EJB.
    If there is any case where error occured due to SQLException, the EJbException is thrown with nested exceptions which are TransactionRollBackException and SQLException.
    Now, when I check EJBException isInstanceOf(SQlException) it gives False.

    Can anybody suggest me how should I check that EjbException thrown is due to SQLException.

    AMit BAdheka
  2. RE: nested Exception Handling[ Go to top ]


    the solution is in the title.. the exception you need to check is nested, i.e. you must access it throught getCause():

    catch(javax.ejb.EJBException e)
        if(e.getCause() instanceof SQlException)

    Depending on what your code looks like, you could also catch the EJBException early on by putting a try-catch clause around the call to the EJB, and then throw e.getCause(). You can then have a standard catch() clause at the end of your code block, without worrying about instanceof.

    While I am at it, may I point out that I have had problems with instanceof in one of my J2EE app, where A instanceof B returned false, even tho A extends B. My suspicion is that the problem is due to A and B being loaded by 2 different class loaders. I'd love it if some smart a*se out there could confirm my suspicions.