JavaFX 8 Global Exception Handling (Lombard)

I'm currently trying to build an application with JavaFX 8, but I cannot get the exception handler to work. Due to this post ( https://bugs.openjdk.java.net/browse/JDK-8100937 ) it should be fixed / implemented using JavaFX 8 (Lombard), but I can not find anything on a clean ...

I don’t want to go in a hacky way, could you give me a hint where to look for additional information?

+4
source share
1 answer

As I understand it, there is nothing special; you just use the usual exception handling of java.lang.Thread.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class UncaughtExceptionTest extends Application {

    @Override
    public void start(Stage primaryStage) {

        // start is called on the FX Application Thread, 
        // so Thread.currentThread() is the FX application thread:
        Thread.currentThread().setUncaughtExceptionHandler((thread, throwable) -> {
            System.out.println("Handler caught exception: "+throwable.getMessage());
        });

        StackPane root = new StackPane();
        Button button = new Button("Throw exception");
        button.setOnAction(event -> {
            throw new RuntimeException("Boom!") ;
        });
        root.getChildren().add(button);
        Scene scene = new Scene(root, 150, 60);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
+10
source

Source: https://habr.com/ru/post/1540245/


All Articles