这里有一些代码可以捕获Event Dispatch Thread上抛出的异常:
package com.ndh.swingjunk;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class EntryPoint {
public static void main(String[] args) {
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
// System.setProperty("sun.awt.exception.handler", MyExceptionHandler.class.getName());
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new SomeWindow("foo").setVisible(true);
}
});
}
}
class SomeWindow extends JFrame {
public SomeWindow(String title) {
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
throw new RuntimeException("hello");
}
}
Run Code Online (Sandbox Code Playgroud)
我已经看到警告,事件调度线程抛出的异常不会被UncaughtExceptionHandler处理,但对我的例子来说似乎并非如此; 无论注册行是注释掉还是遗留下来,它的工作方式都是一样的.我的示例是以某种方式搞砸了,还是注册了sun.awt.exception.handler不再需要的异常处理程序?
在摇摆我们有:
boolean SwingUtilities.isEventDispatchThread()
Run Code Online (Sandbox Code Playgroud)
如果从 Swing 事件调度线程调用,则返回 true。
javafx 中有类似的东西吗?