java Throwable 和 Exception 的差別

Note that an Error is not an Exception; it’s a Throwable.
So, if you catch ExceptionErrors will still get through:

private void m() {    
    try {
        m(); // recursively calling m() will throw a StackOverflowError
    } catch (Exception e) {
        // this block won't get executed, 
        // because StackOverflowError is not an Exception!

to catch “everything”, change your code to this:

try {
} catch (Throwable e) {
   // this block will execute when anything "bad" happens

Note that there might be little you can do if an Error occurs. Excerpt from javadoc for Error:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a “normal” condition, is also a subclass of Error because most applications should not try to catch it.



發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *