JavaFX 8(Lombard)全局异常处理 [英] JavaFX 8 (Lombard) global exception handling

查看:407
本文介绍了JavaFX 8(Lombard)全局异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不想走黑路,您能否提示我在哪里搜索更多信息?

I don't want to go the hackish way, may you give me a hint where to search for further information?

推荐答案

据我了解,它没什么大不了的.您只需使用来自java.lang.Thread的常规未捕获异常处理.

As I understand it, there's nothing much to it; you just use the regular uncaught exception handling from 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);
    }
}

这篇关于JavaFX 8(Lombard)全局异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆