Catching exceptions in code executed by EventQueue
1 posts in topic
Flat View  Flat View

Posted By:   Patrick_Stephens
Posted On:   Thursday, March 29, 2001 07:19 AM

If application code throws an exception which it does not itself catch, there is a crash in the current EventQueue DispatchThread. So although to the user the application may appear to continue to run to some extent, really this is an internal error in the application which should be reported to the user and the application terminated.

It is impractical to put a try-catch around every possible event handler in the application. So the question... is it somehow possible to catch exceptions within the EventQueue's DispatchThread and execute some code? I vaguely see some possibility of putting the dispatch threads in a thread group and using ThreadGroup.uncaughtException()???

Re: Catching exceptions in code executed by EventQueue

Posted By:   Peter_Gadjokov  
Posted On:   Tuesday, May 15, 2001 08:07 PM

Quoted below is the is the documentation of the private method handleException of java.awt.EventDispatchThread. It's not a long-term solution but it does work.

Handles an exception thrown in the event-dispatch thread.

If the system property "sun.awt.exception.handler" is defined, then
when this method is invoked it will attempt to do the following:

  1. Load the class named by the value of that property, using the
    current thread's context class loader,
  2. Instantiate that class using its zero-argument constructor,
  3. Find the resulting handler object's public void handle
    method, which should take a single argument of type
    Throwable, and
  4. Invoke the handler's handle method, passing it the
    thrown argument that was passed to this method.

If any of the first three steps fail then this method will return
false and all following invocations of this method will return
false immediately. An exception thrown by the handler object's
handle will be caught, and will cause this method to return
false. If the handler's handle method is successfully
invoked, then this method will return true. This method will
never throw any sort of exception.

Note: This method is a temporary hack to work around the
absence of a real API that provides the ability to replace the
event-dispatch thread. The magic "sun.awt.exception.handler" property
will be removed in a future release.

About | Sitemap | Contact