JavaFX 8(Lombard)全局异常处理

bas*_*h0r 5 java exception-handling javafx-8

我目前正在尝试使用JavaFX 8构建一个应用程序,但我无法使未捕获的异常处理工作.由于这篇文章(https://bugs.openjdk.java.net/browse/JDK-8100937),它应该用JavaFX 8(Lombard)修复/实现,但我在网上找不到任何东西......

我不想采取hack的方式,你能否给我一个提示,在哪里寻找更多信息?

Jam*_*s_D 12

据我了解,没有什么可以做的; 您只需使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)