gpt4 book ai didi

java - 如果直接调用 Thread.getUncaughtExceptionHandler().uncaughtException 会发生什么不好的事情?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:34 26 4
gpt4 key购买 nike

将已检查的异常直接丢弃到 UncaughtExceptionHandler 的负面后果是什么?

        Thread t = new Thread() {

@Override
public void run() {
try {
verb();
} catch (Exception e) {
getUncaughtExceptionHandler().uncaughtException(this,e);
}
}
};

显然,应用程序有(应该有)后果——编写 verb() 的人认为应该处理这种特殊情况。假设我愿意忽略它,还有什么地方会出错?

在线程即将终止之前调用 ExceptionHandler 会产生什么后果吗?

最佳答案

不应该有负序。
如果您没有明确设置自定义处理程序,则 ThreadGroup 方法将是称为 which :

This method determines if the Throwable argument is an instance of ThreadDeath. If so, nothing special is done. Otherwise, a message containing the thread's name, as returned from the thread's getName method, and a stack backtrace, using the Throwable's printStackTrace method, is printed to the standard error stream.

如果您设置了自定义设置,最有可能的是它只会记录一些内容。所以你只会看到线程日志和堆栈跟踪。
除非您的自定义处理程序执行仅在线程终止时才应执行的特定操作,但从 OP 看来情况并非如此。

关于java - 如果直接调用 Thread.getUncaughtExceptionHandler().uncaughtException 会发生什么不好的事情?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15796813/

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