0

I have a method that throws an MalformedURLException. I catch the exception with try-catch.
But instead of catching the MalformedURLException, it jumps to the EJBException.

try {
    myMethod(); // throws an exception
} catch (WebApplicationException ex) {
    ex.printStackTrace();
} catch (MalformedURLException ex) {
    ex.printStackTrace(); // I expected the debugger to jump to that line
} catch (EJBException ex) {
    ex.printStackTrace(); // debugger jumps to this line
} catch (Exception ex) {
    ex.printStackTrace();
}

StackTrace:

WARNING:   A system exception occurred during an invocation on EJB MyClass, method: public com.name.AnotherClass com.name.MyClass.myMethod(java.lang.String,java.lang.String) throws java.net.MalformedURLException
WARNING:   javax.ejb.EJBException
    [...] // much more text...
Caused by: javax.ws.rs.WebApplicationException: HTTP 400 Bad Request
    [...] // much more text...
FATAL:   javax.ejb.EJBException
    [...] // much more text...
Caused by: javax.ws.rs.WebApplicationException: HTTP 400 Bad Request
    [...] // much more text...

As stated here:

If the first catch matches the exception, it executes, if it doesn't, the next one is tried and on and on until one is matched or none are.

In my example MalformedURLException fires first, as you can see in the 1st line:

throws java.net.MalformedURLException

Only the 2nd line says:

javax.ejb.EJBException

So, why does it immediately jumps to EJBException, if MalformedURLException fires first?

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181

1 Answers1

2

Check of the EJBException's ex.getCause(). The MalformedURLException is wrapped by the EJB container's magic.

One might imagine somewhere a

try { ...
} catch (Exception ex) {
    throw new EJBException(ex); // Cause: ex.
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • There is no suitable constructor found. That would work: `EJBException((Exception) ex)`. Is it what you mean? – Evgenij Reznik Feb 14 '19 at 12:46
  • 1
    You are right; corrected from Throwable to Exception. (I did not intend for you to use this. Just that such a kind of wrapping happens.) – Joop Eggen Feb 14 '19 at 13:27