Duplicate :
|
It is impossible to catch an unexpected RuntimeException from an ActionEvent in a generic way. A customized response to that exceptions is impossible, which is critical for us because our application is a 24/7 app for a power plant control system. Here is a sample app: ============================================ import java.lang.Thread.UncaughtExceptionHandler; import javafx.animation.PauseTransition; import javafx.application.Application; import javafx.beans.property.IntegerProperty; import javafx.beans.property.SimpleIntegerProperty; import javafx.stage.Stage; import javafx.util.Duration; public class ListenerExceptionTest extends Application { @Override public void start(Stage stage) throws Exception { Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("Caught in DefaultUncaughtExceptionHandler: " + t + " " + e); } }); IntegerProperty counter = new SimpleIntegerProperty(0); counter.addListener(o -> { throw new RuntimeException("Unexpected RuntimeException in InvalidationListener"); }); PauseTransition counterIncrement = new PauseTransition(Duration.seconds(1)); try { counterIncrement.setOnFinished(e -> { counter.set(counter.get() + 1); counterIncrement.playFromStart(); throw new RuntimeException("Unexpected RuntimeException in ActionEvent"); }); } catch (Exception e) { System.out.println("Caught from setOnFinished: " + e); } counterIncrement.playFromStart(); stage.show(); } public static void main(String[] args) { try { launch(args); } catch (Exception e) { System.out.println("Caught in main: " + e); } } } ============================================ It results in: ============================================ Caught in DefaultUncaughtExceptionHandler: Thread[JavaFX Application Thread,5,main] java.lang.RuntimeException: Unexpected RuntimeException in InvalidationListener java.lang.RuntimeException: Unexpected RuntimeException in ActionEvent at de.fhg.iwes.javafx.vpp.frontend.ListenerExceptionTest.lambda$1(ListenerExceptionTest.java:35) at de.fhg.iwes.javafx.vpp.frontend.ListenerExceptionTest$$Lambda$56/2077198483.handle(Unknown Source) at javafx.animation.Animation.impl_finished(Animation.java:1132) at javafx.animation.AnimationAccessorImpl.finished(AnimationAccessorImpl.java:49) at com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(SingleLoopClipEnvelope.java:103) at javafx.animation.Animation.impl_timePulse(Animation.java:1102) at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186) at javafx.animation.Animation$1$$Lambda$72/2057789281.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at javafx.animation.Animation$1.timePulse(Animation.java:185) at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344) at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267) at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:447) at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:431) at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$363(QuantumToolkit.java:298) at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$41/28432492.run(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102) at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source) at java.lang.Thread.run(Thread.java:745)