这里有一些代码可以捕获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不再需要的异常处理程序?
我正在javax.swing制作一个应用程序,它从XML Schema(使用JAXFront库)生成表单,并将用户填充的数据存储到XML文档中.
我在需要时放了try-catch-finally块,但是当主线程结束时(AWT线程仍然在运行),我遇到了一些问题.
我有两个主要工作的课程和其他对这个问题不重要的课程:
主类:它具有以下结构.初始化应用程序并运行主框架
public class Main {
public static void main(String[] args) {
readArgs(); // An INI file with the app config
Model model = initializeElements(args); // My model class
try {
MyFrame mfr = new MyFrame(title,model);
mfr.visualize(); // Assembling view and setting visible
} catch( Excepion e ) {
doCleanUp();
System.exit(-1);
}
}
}Run Code Online (Sandbox Code Playgroud)帧类:生成视图和侦听事件
public class MyFrame extends JFrame implements ActionListener,MenuListener {
// Some attributes
// Other mthods without importance
/**
* …Run Code Online (Sandbox Code Playgroud)