JavaFX 2 - 捕捉所有运行时异常 [英] JavaFX 2 - Catching all runtime exceptions

查看:733
本文介绍了JavaFX 2 - 捕捉所有运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过

  Thread.setDefaultUncaughtExceptionHandler ... 
/ pre>



在主要,也在开始(Stage primaryStage)方法。它不工作。

我也尝试过

  public static void main(String [ ] args){
try {
launch(args);
} catch(Throwable t){
System.out.println(t.getMessage);
}
}




异常堆栈跟踪。


在javafx.concurrent.Task $ TaskCallable $ 2.run(Task.java:1251 )
com.sun.javafx.application.PlatformImpl $ 3.run(PlatformImpl.java:141)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)at
com.sun.glass.ui.gtk.GtkApplication $ 1 $ 1.run(GtkApplication.java:56)
在java.lang.Thread.run(Thread.java:662)


感谢您的帮助。

解决方案

对于 Platform.runLater()(见下文),您会看到异常被吞噬(第146/147行),因此默认未捕获的异常处理程序将无法抓住它们 - 基于这段代码,我不认为你有任何选择,但是在你的runnable中包含try / catch块。



请注意,此问题已被报告(需要登录 - 注册是免费的),并应在Lombard( = Java FX 8.0将于明年发布Java 8)



您可以创建一个实用程序方法并调用

  Platform.runLater(getFxWrapper(yourRunnable)); 

public static Runnable getFxWrapper(final Runnable r){
return new Runnable(){

@Override
public void run(){
try {
r.run();
} catch(Exception e){
//这里你可能想要记录一些东西
System.out.println(Found an exception);
}
}
};
}

代码 Platform.runLater

  120 private static void runLater(final Runnable r,boolean exiting){ 
121 if(!initialized.get()){
122 throw new IllegalStateException(Toolkit not initialized);
123}
124
125 pendingRunnables.incrementAndGet();
126 waitForStart();
127
128 if(SystemProperties.isDebug()){
129 Toolkit.getToolkit()。pauseCurrentThread();
130}
131
132 synchronized(runLaterLock){
133 if(!exiting&& toolkitExit.get()){
134 // Don'在我们退出工具包
135 pendingRunnables.decrementAndGet();
136返回;
137}
138
139 Toolkit.getToolkit()。defer(new Runnable(){
140 @Override public void run(){
141 try {
142 r.run();
143 pendingRunnables.decrementAndGet();
144 checkIdle();
145} catch(Throwable t){
146 System.err .println(runnable中的异常);
147 t.printStackTrace();
148}
149}
150});
151}
152}


I tried

Thread.setDefaultUncaughtExceptionHandler...


in the main, and also in the start(Stage primaryStage) method. It ain't working.
I also tried

public static void main(String[] args) {
 try {
  launch(args);
 }catch(Throwable t) {
  System.out.println(t.getMessage);
 }
}


Exception stack trace.

at javafx.concurrent.Task$TaskCallable$2.run(Task.java:1251) at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) at com.sun.glass.ui.gtk.GtkApplication$1$1.run(GtkApplication.java:56) at java.lang.Thread.run(Thread.java:662)

Thanks for helping.

解决方案

If you check the code for Platform.runLater() (see below), you will see that the exceptions are swallowed (lines 146 / 147), so a default uncaught exception handler won't be able to catch them - based on that piece of code, I don't think you have any options but to include try/catch blocks in your runnables.

Note that this issue has been reported (requires login - registration is free) and should be fixed in Lombard (= Java FX 8.0 to be released with Java 8 next year).

You could alternatively create a utility method and call

Platform.runLater(getFxWrapper(yourRunnable));

public static Runnable getFxWrapper(final Runnable r) {
    return new Runnable() {

        @Override
        public void run() {
            try {
                r.run();
            } catch (Exception e) {
                //here you probably want to log something
                System.out.println("Found an exception");
            }
        }
    };
}

Code of Platform.runLater:

  120     private static void runLater(final Runnable r, boolean exiting) {
  121         if (!initialized.get()) {
  122             throw new IllegalStateException("Toolkit not initialized");
  123         }
  124 
  125         pendingRunnables.incrementAndGet();
  126         waitForStart();
  127 
  128         if (SystemProperties.isDebug()) {
  129             Toolkit.getToolkit().pauseCurrentThread();
  130         }
  131 
  132         synchronized (runLaterLock) {
  133             if (!exiting && toolkitExit.get()) {
  134                 // Don't schedule a runnable after we have exited the toolkit
  135                 pendingRunnables.decrementAndGet();
  136                 return;
  137             }
  138 
  139             Toolkit.getToolkit().defer(new Runnable() {
  140                 @Override public void run() {
  141                     try {
  142                         r.run();
  143                         pendingRunnables.decrementAndGet();
  144                         checkIdle();
  145                     } catch (Throwable t) {
  146                         System.err.println("Exception in runnable");
  147                         t.printStackTrace();
  148                     }
  149                 }
  150             });
  151         }
  152     }

这篇关于JavaFX 2 - 捕捉所有运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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