Java异常没有被捕获

Java异常没有被捕获

本文介绍了Java异常没有被捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java中的一些异常没有被 catch(Exception ex)捕获?这是代码完全失败与未处理的异常。 (Java版本1.4)。

  public static void main(String [] args){
try {
// Code ...
} catch(Exception ex){
System.err.println(Caught Exception);
ex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
}
finally {
app.shutdown();
}
System.exit(exitCode);
}

我得到一个线程中的异常mainjava .lang.NoSuchMethodError



但这个工作原理

  public static void main(String [] args){
int exitCode = app.SUCCESS_EXIT_CODE;
try {
// Code ...
} catch(java.lang.NoSuchMethodError mex){
System.err.println(Caught NoSuchMethodError);
mex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
} catch(Exception ex){
System.err.println(Caught Exception);
ex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
}
finally {
app.shutdown();
}
System.exit(exitCode);
}

我得到捕获NoSuchMethodError java.lang.NoSuchMethodError:我认为捕获的异常会捕获所有的异常?如何捕获java中的所有异常?

解决方案

因为某些例外不是从 / code> - 例如 Throwable 错误



基本上类型层次结构:

  Object 
|
Throwable
/ \
异常错误

只有 Throwables 和派生类可以抛出,所以如果你抓住 Throwable ,那真的会抓住所有的东西。



异常导出的任何异常(或异常本身)其他比那些派生自 RuntimeException 计数为检查异常的那些 - 它们是你必须声明的,你会抛出,或捕获如果你打电话给他们的东西。



所有人都说,Java异常层次结构有点混乱...


Why are some exceptions in Java not caught by catch (Exception ex)? This is code is completely failing out with an unhandled exception. (Java Version 1.4).

public static void main(String[] args) {
    try {
        //Code ...
    } catch (Exception ex) {
        System.err.println("Caught Exception");
        ex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    }
    finally {
        app.shutdown();
    }
    System.exit(exitCode);
}

I get a Exception in thread "main" java.lang.NoSuchMethodError

But this works

public static void main(String[] args) {
    int exitCode = app.SUCCESS_EXIT_CODE;
    try {
        //Code ...
    } catch (java.lang.NoSuchMethodError mex){
        System.err.println("Caught NoSuchMethodError");
        mex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    } catch (Exception ex) {
        System.err.println("Caught Exception");
        ex.printStackTrace();
        exitCode = app.FAILURE_EXIT_CODE;
    }
    finally {
        app.shutdown();
    }
    System.exit(exitCode);
}

I get Caught NoSuchMethodError java.lang.NoSuchMethodError:

I thought catching exceptions would catch all exceptions? How can I catch all exceptions in java?

解决方案

Because some exceptions don't derive from Exception - e.g. Throwable and Error.

Basically the type hierarchy is:

       Object
         |
      Throwable
     /         \
Exception      Error

Only Throwables and derived classes can be thrown, so if you catch Throwable, that really will catch everything.

Any exception deriving from Exception (or Exception itself) other than those derived from RuntimeException count as checked exceptions - they're the ones that you have to declare you'll throw, or catch if you call something that throws them.

All told, the Java exception hierarchy is a bit of a mess...

这篇关于Java异常没有被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:20