在javax.swing应用程序中捕获异常 [英] Catch exceptions in javax.swing application

查看:196
本文介绍了在javax.swing应用程序中捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 javax.swing 来创建一个从XML Schema生成表单的应用程序(使用JAXFront库),并将用户填写的数据存储到XML中文件。



当我需要它时,我已经放入try-catch-finally块,但主线程结束时捕获异常有一些问题(AWT线程仍然存在运行)。



我有两个课程,主要工作和其他类不重要的问题:




  • 主类 :它具有以下结构。初始化应用程序并运行主框架

      public class Main {
    public static void main(String [] args) {
    readArgs(); //具有app config INI文件
    模型model = initializeElements(args); //我的模型类
    try {
    MyFrame mfr = new MyFrame(title,model);
    mfr.visualize(); //组装视图和设置可见
    } catch(Excepion e){
    doCleanUp();
    System.exit(-1);
    }
    }
    }


  • Class :生成视图和听事件

      public class MyFrame extends JFrame implements ActionListener,MenuListener { 
    //某些属性
    //其他没有重要性的主体
    / **
    *组成元素,添加听众并设置可见的框架
    * /
    public void visualize(){
    generateFormPanel();
    setListeners();
    validate();
    setVisible(true);
    }

    public MyFrame(String title,Modele model){
    super(title);
    createElementsUsing(model);
    }

    public void actionPerformed(ActionEvent e){
    //管理事件的代码
    }
    }
    pre>



嗯,问题是如下:
当从main方法,视图生成和显示。那时候我失去了捕捉异常的控制。那么我的问题是,如果有一些方法可以捕获在这一点之后抛出的可能的RuntimeExceptions



我希望你了解我的英语,并且可以回答



提前感谢。

解决方案

最简单的版本是设置默认未捕获的异常处理程序:

  Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
public void uncaughtException (Thread t,Throwable e){
// do something
}
});

但是,在程序的其他部分抛出未捕获的异常。



然而,您可以仅使用代理程序捕获swing事件调度线程抛出的运行时异常(请参阅页面了解更多信息,复制代码):

  class EventQueueProxy extends EventQueue {

protected void dispatchEvent(AWTEvent newEvent){
try {
super.dispatchEvent(newEvent );
} catch(Throwable t){
//做一些比t.printStackTrace()更有用的东西;
}
}
}

现在安装如下:

  Toolkit.getDefaultToolkit()。getSystemEventQueue()。push(new EventQueueProxy()); 


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.

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
        }
    }

Well, the problem is the following: When the visualize function is exectuted from the main method, the view is generated and showed. At that moment is when I lose the control of the exceptions catching. Then my question is if there are some way to catch the possible RuntimeExceptions throwed after this point.

I hope you understand my English and can answer the question.

Thanks in advance.

解决方案

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.

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();
        }
    }
}

Now installing it like this:

Toolkit.getDefaultToolkit().getSystemEventQueue().push(new EventQueueProxy());

这篇关于在javax.swing应用程序中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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