2

更新:正如下面的评论中所指出的,JavaFx8 将处理未捕获的运行时异常——但是现在如何处理它们呢?目前他们只是转到 Java 控制台,所以在这种情况下,单击按钮只会让它静默失败。如果弹出对话框至少可以提醒用户出现问题,那就太好了。有什么想法吗?

较早的问题: 在我的 JavaFX2 应用程序中,我尝试设置一个从未被调用的未捕获异常处理程序。下面,您可以看到由 Null Pointer Exception 启动的堆栈跟踪。跟踪会自动打印在 NetBeans 7.3 控制台中。但是,我的处理程序永远不会被调用。

 Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                setFailed(e);
            }
        });

堆栈跟踪:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
         at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1440)
         at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69)
         at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
         at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
         at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
         at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
         at javafx.event.Event.fireEvent(Event.java:171)
         at javafx.scene.Node.fireEvent(Node.java:6863)
         at javafx.scene.control.Button.fire(Button.java:179)
         at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193)
         at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336)
         at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329)
         at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64)
         at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217)
         at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170)
         at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
         at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
         at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53)
         at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
         at javafx.event.Event.fireEvent(Event.java:171)
         at javafx.scene.Scene$MouseHandler.process(Scene.java:3328)
         at javafx.scene.Scene$MouseHandler.process(Scene.java:3168)
         at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3123)
         at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563)
         at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2265)
         at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:250)
         at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:173)
         at java.security.AccessController.doPrivileged(Native Method)
         at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292)
         at com.sun.glass.ui.View.handleMouseEvent(View.java:528)
         at com.sun.glass.ui.View.notifyMouse(View.java:922)
         at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
         at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
         at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
         at java.lang.Thread.run(Thread.java:722)
Caused by:
java.lang.reflect.InvocationTargetException
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:601)
         at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:55)
         at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
         at java.lang.reflect.Method.invoke(Method.java:601)
         at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:269)
         at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1437)
         ... 64 more
4

3 回答 3

3

永远不会调用您的处理程序的原因是它仅用于“未捕获”异常。Java FX 事件系统是一个不允许死亡的线程,因此它会捕获其中抛出的所有异常,包括事件处理程序中的异常,并将它们记录到控制台/记录器。

如本答案所述,Java 8 中有一个解决方案:JavaFX 2 - 捕获所有运行时异常

于 2013-05-23T17:18:41.653 回答
2

功能RT-15332 允许应用程序使用 UncaughtExceptionHandler 捕获由 FX 应用程序线程引发的异常,最近已为Java 8实现。

JavaFX 8 之前的版本没有相应的功能。

于 2013-05-23T17:17:02.540 回答
0

它可能是另一个线程。使用setDefaultUncaughtExceptionHandler,它不会触及可能存在的处理程序。

或者在 JavaFX 线程中设置处理程序。

于 2013-05-23T17:08:56.003 回答