2

Update: As indicated in the comments below, JavaFx8 will handle uncaught run-time exceptions - but what to do about them now? Presently they just go to the Java console so in this case, clicking the button just makes it fail silently. It would be nice if a popup dialog would at least alert the user that something went wrong. Any ideas how?

Earlier question: In my JavaFX2 application, I try to set an uncaught exception handler which never gets called. Below, you can see the stack trace initiated by a Null Pointer Exception. The trace is automatically printed in the NetBeans 7.3 console. However, my handler is never called.

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

Stack trace:

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
likejudo
  • 3,396
  • 6
  • 52
  • 107

3 Answers3

3

The reason that your handler is never called is that it is only used on "uncaught" exceptions. The Java FX event system is a thread that cannot be allowed to die, so it catches all exceptions thrown within it, including the ones in your event handlers, and logs them to the console/logger.

There is a solution to this in Java 8 as stated in this answer: JavaFX 2 - Catching all runtime exceptions

Community
  • 1
  • 1
Erik Nedwidek
  • 6,134
  • 1
  • 25
  • 25
  • The solution is to catch your exceptions. The uncaught exception handler should only ever be considered a bandaid to keep a program from dying. The correct thing to do is to handle exceptions with try/catch at the source. That is the only point to where you can do something sensible with the exception. The uncaught exception handler should only be used to find those spots where you missed catching an exception or where a RTE is occuring. – Erik Nedwidek May 24 '13 at 18:00
2

Feature RT-15332 Allow application to catch exceptions thrown by FX application thread with an UncaughtExceptionHandler was recently implemented for Java 8.

There is no corresponding functionality for JavaFX versions prior to 8.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
0

It could be another thread. Use setDefaultUncaughtExceptionHandler, which will not touch possibly existing handlers.

Or set the handler in the JavaFX thread.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • setDefaultUncaughtExceptionHandler: "Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread". My handler is set on the JavaFx thread. – likejudo May 24 '13 at 14:25