JDK-8088773 : JavaFX does not fully support Java UncaughtExceptionHandler
  • Type: Bug
  • Component: javafx
  • Sub-Component: application-lifecycle
  • Affected Version: 8u25
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2014-12-02
  • Updated: 2018-09-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Although RT-15332 has been closed, I believe I have proof that the issue is not yet solved. JavaFX still catches all exceptions at least in 1 situation (there may be more, and I suggest you look for them). Here is the problematic code, I have found in file LauncherImpl:

                PlatformImpl.runAndWait(() -> {
                    try {
                        startCalled.set(true);

                        // Create primary stage and call application start method
                        final Stage primaryStage = new Stage();
                        primaryStage.impl_setPrimary(true);
                        theApp.start(primaryStage);
                    } catch (Throwable t) {
                        System.err.println("Exception in Application start method");
                        startError = t;
                        error = true;
                    }
                });

As you can see, when theApp.start() is invoked, the code above catches all exceptions, thereby preventing our application code from carrying out its traditional Java default exception handling. You can verify the bug by running the test sample below.

I think this bug is not to be taken lightly. Many professional applications implement default handling of uncaught exceptions that carry out tasks which are important. In our app for instance, if an uncaught exception occurs, our app will log the exception in a database and have our server send an e-mail to the quality assurance team. However, when JavaFX steps in, and grabs all the exceptions, this does not happen.

If you want JavaFX to catch uncaught exceptions, please follow the traditional Java pattern for doing so, thereby allowing us to replace your default handling with ours.

public class ExceptionTest extends Application {
	
	private Thread.UncaughtExceptionHandler uncaughtHandler = 
		new Thread.UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread thread, final Throwable exception) {
				System.out.println("Caught uncaught exception.");
			}
		};
		
	@Override
	public void start(Stage primaryStage) throws Exception {
		Thread.setDefaultUncaughtExceptionHandler(uncaughtHandler);
		Thread.currentThread().setUncaughtExceptionHandler(uncaughtHandler);
		Platform.runLater(() -> {
			Thread.currentThread().setUncaughtExceptionHandler(uncaughtHandler);
		});
		throw new RuntimeException("This is never caught by the handler as it should.");
	}

	public static void main(String... arguments) {
		launch(ExceptionTest.class, arguments);
	}
}


Comments
Since we are at RDP1 I am moving this out of 9. I would still like to fix it for 9 if a safe fix can be found.
01-09-2016

Superb. I am really looking forward to this improvement, Kevin.
12-11-2015

I took a quick look at this, and I think it should be possible to do what you want, but not the way you are suggesting. The JavaFX runtime must wrap the call to Application.start in a try/catch, both because we need to control the flow to implement the specified lifecycle, and more importantly because it won't compile without it -- the start method has "throws Exception" in it's signature. What we can likely do is what we do in other cases, namely to call the UncaughtExceptionHandler if one has been defined. This might need to be done in deploy code for Java Web Start applications as well as in LauncherImpl. We will consider this for JDK 9 if there are no complications with the above approach (I can't think of any).
12-11-2015

We need to ensure the control flow of the application life-cycle in the face of exceptions, so this might be difficult to do, but I will look into it for FX 9.
03-12-2014