gpt4 book ai didi

java - UncaughtExceptionHandler 未捕获异常

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:56 24 4
gpt4 key购买 nike

我不确定为什么未调用 uncaughtException 方法。

static
{
/**
* Register a logger for unhandled exceptions.
*/
Thread.UncaughtExceptionHandler globalExceptionHandler = new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException(Thread t, Throwable e)
{
System.out.println("handle exception."); // can also set bp here that is not hit.
}
};

Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
Thread.currentThread().setUncaughtExceptionHandler(globalExceptionHandler);

/**
* Register gateway listen port.
*/
try
{
// some stuff that raises an IOException
}
catch (IOException e)
{
System.out.println("Throwing exception");
throw new RuntimeException(e);
}

}

程序输出为:

抛出异常

java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: blah.jks
(The system cannot find the file specified)
...some stack trace...
Exception in thread "main"
Process finished with exit code 1

最佳答案

RuntimeException 从静态初始值设定项中引发,它发生在加载主类时。然后它被系统类加载器捕获,将其包装到一个ExceptionInInitializerError中,然后从 JVM 退出。由于捕获了异常,因此永远不会调用默认的未捕获异常处理程序。

关于java - UncaughtExceptionHandler 未捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27696018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com