gpt4 book ai didi

java - UncaughtExceptionHandler 没有捕获到一些异常

转载 作者:行者123 更新时间:2023-11-30 11:47:43 26 4
gpt4 key购买 nike

我创建了一个 UncaughtExceptionHandler,如 this article 所示。 .

我还注册了这个处理程序来捕获所有线程中的异常,如下所示:

    Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());

但是,它缺少一些异常(exception):

Exception occurred during event dispatching:
java.lang.RuntimeException: Critical error!
at com.acme.MyClass.myMethod(MyClass.java:46)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:611)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)
at java.awt.Dialog$1.run(Dialog.java:1046)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

为什么会遗漏这样的异常?

最佳答案

可能抛出此异常的线程已经有自己的未捕获异常处理程序。在这种情况下,不使用默认处理程序。您可以通过在相关线程(这里是事件调度程序线程)上调用 getUncaughtExceptionHandler() 方法来检查这一点。

也可能是这个线程属于一个特殊的ThreadGroup,它处理异常而不是将其委托(delegate)给默认处理程序。

在这两种情况下,您都可以使用 setUncaughtExceptionHandler() 方法将您的处理程序显式设置为该线程的处理程序。

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

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