Catch exceptions in javax.swing application

前端 未结 3 426
醉酒成梦
醉酒成梦 2021-01-12 19:41

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 i

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 19:47

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

提交回复
热议问题