2

The following code:

try {
    value = parse(myData);
} catch (Exception e) {
    if ( e instanceof IOException|| e instanceof IllegalArgumentException) {
       logger.debug("illegal argument");
    } else {
       logger.debug("this is printing");
    }
}

Parse method:

parse(String data) throws IOException, IllegalArgumentException {
   // do validation
    throw new IllegalArgumentException("illegal");
}

I was expecting "Illegal argument". But instead it shows "this is printing".

Did I miss anything here?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173

2 Answers2

2

I traced it. When this IllegalArgumentException is thrown, its wrapped by EJBTransactionRollbackException.

The following is thrown:

The transaction has been marked rollback only because the bean encountered a non-application 
exception :java.lang.IllegalArgumentException: 
Actual Exception class: class javax.ejb.EJBTransactionRolledbackException

Now the question is different, how not to have EJBTransactionException override the actual exception

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
  • 2
    Overriden is not then right word for this. The `EJBTransactionRollbackException` wraps the `IllegalArgumentException` and so the types don't match. You can just catch the exception and do `getCause()` to get yours. – Sotirios Delimanolis Jul 09 '13 at 20:42
0

Just an idea. Are you sure you use classes from the same package on throw and catch sections. For example you can throw com.foo.bar.IllegalArgumentException in parse method but trying to catch java.lang.IllegalArgumentException So could you please check import section in case you have them in different classes.

kaktooss
  • 41
  • 3