例外顺序
Order of Exceptions
我有一个抛出 MalformedURLException
的方法。我用 try-catch
.
捕获了异常
但是它没有捕获 MalformedURLException
,而是跳转到 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();
}
堆栈跟踪:
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...
如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.
在我的示例中,MalformedURLException
首先触发,如您在第一行中所见:
throws java.net.MalformedURLException
只有第二行说:
javax.ejb.EJBException
那么,如果 MalformedURLException
先触发,为什么它会立即跳转到 EJBException
?
检查 EJBException 的 ex.getCause()
。 MalformedURLException 由 EJB 容器的魔法包装。
人们可能会想象某个地方
try { ...
} catch (Exception ex) {
throw new EJBException(ex); // Cause: ex.
}
我有一个抛出 MalformedURLException
的方法。我用 try-catch
.
捕获了异常
但是它没有捕获 MalformedURLException
,而是跳转到 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();
}
堆栈跟踪:
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...
如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.
在我的示例中,MalformedURLException
首先触发,如您在第一行中所见:
throws java.net.MalformedURLException
只有第二行说:
javax.ejb.EJBException
那么,如果 MalformedURLException
先触发,为什么它会立即跳转到 EJBException
?
检查 EJBException 的 ex.getCause()
。 MalformedURLException 由 EJB 容器的魔法包装。
人们可能会想象某个地方
try { ...
} catch (Exception ex) {
throw new EJBException(ex); // Cause: ex.
}