javax.swing中的应用程序捕获异常 [英] Catch exceptions in javax.swing application
问题描述
我与的javax.swing
努力使生成(使用JAXFront库)从XML架构形式和存储由它们的用户转换成XML填充数据的aplication文档。
I'm working with javax.swing
to make an aplication which generates forms from XML Schema (using JAXFront library) and stores the data filled by the user them into XML documents.
我已经把的try-catch-finally块,当我需要它,但我有一个小问题,捕获异常时,主线程结束(AWT的线程仍在运行)。
I have put try-catch-finally blocks when I need it, but I have a little problem catching exceptions when the main thread ends (The AWT threads are still running).
我有两个类里面做的主要工作和其它类这是不是问题非常重要:
I have two classes which do the main work and other classes which aren't important for the question:
-
的 主要类的:它具有以下的结构。初始化应用程序并运行主框架
Main class: It has the following structure. Initializes the application and runs the main frame
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);
}
}
}
的 框架类的:生成的看法,并听取事件
Frame Class: Generates the view and listen events
public class MyFrame extends JFrame implements ActionListener,MenuListener {
// Some attributes
// Other mthods without importance
/**
* Compose the elements, add listeners and set visible the frame
*/
public void visualize() {
generateFormPanel();
setListeners();
validate();
setVisible(true);
}
public MyFrame(String title, Modele model) {
super(title);
createElementsUsing(model);
}
public void actionPerformed(ActionEvent e) {
// Code to manage events
}
}
那么,问题如下:
当形象化功能从主方法exectuted,生成并显示图。在那一刻,是当我失去了追赶的异常的控制。然后我的问题是,如果有一些办法赶上了这一点后,倒掉的可能RuntimeExceptions
我希望你明白我的英语水平能回答这个问题。
I hope you understand my English and can answer the question.
先谢谢了。
推荐答案
最简单的版本设置默认的未捕获异常处理程序:
Simplest version is to set the default uncaught exception handler:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
// do something
}
});
不过,捕捉程序中的藏汉的其他部分抛出未捕获的异常。
But that catches uncaught exceptions thrown in other parts of the program aswell.
然而,你能赶上揭去使用代理Swing事件分派线程仅运行时异常(见的此页面了解更多信息,复制$ C $从那里C):
You could however catch only runtime exceptions thrown off the swing event dispatching thread using a proxy (See this page for more information, copied code from there):
class EventQueueProxy extends EventQueue {
protected void dispatchEvent(AWTEvent newEvent) {
try {
super.dispatchEvent(newEvent);
} catch (Throwable t) {
// do something more useful than: t.printStackTrace();
}
}
}
现在安装它是这样的:
Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueueProxy());
这篇关于javax.swing中的应用程序捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!