catch exceptions in EventDispatchThread  
Author Message
bernd_no_junk





PostPosted: 2004-8-20 9:13:00 Top

java-programmer, catch exceptions in EventDispatchThread Hi, I would like to catch all exception thrown
by the EventDispatchThread.
How do I do this?

Greetings

Bernd

 
Alan Moore





PostPosted: 2004-8-20 15:24:00 Top

java-programmer >> catch exceptions in EventDispatchThread On 19 Aug 2004 18:12:32 -0700, email***@***.com wrote:

>Hi, I would like to catch all exception thrown
>by the EventDispatchThread.
>How do I do this?

This approach uses an undocumented feature of EventDispatchThread.
Actually, it is described in the doc for the handleException() method,
but you have to go to the source code to read it. The doc also says
this feature will be removed in a future release (but it's still there
in Tiger).


public class MyApp
{
public static void main(String[] args)
{
System.setProperty("sun.awt.exception.handler",
"MyApp$EDTErrorHandler");

// etc.
}

/**
* This class will be instantiated the first time an
* exception is thrown on the EDT.
*/
public static class EDTErrorHandler
{
/**
* This method is invoked by the AWT event dispatch
* mechanism when an unexpected exception or error
* is thrown during event dispatching.
*/
public void handle(Throwable t)
{
// handle the exception
}
}
}


 
Chris Smith





PostPosted: 2004-8-21 2:05:00 Top

java-programmer >> catch exceptions in EventDispatchThread email***@***.com wrote:
> Hi, I would like to catch all exception thrown
> by the EventDispatchThread.
> How do I do this?

There are a number of approaches. Here's one:

Toolkit.getDefaultToolkit().getSystemEventQueue()
.push(new EventQueue() {
protected void dispatchEvent(AWTEvent event)
{
try
{
super.dispatchEvent(event);
}
catch (Exception e)
{
handleException(e);
}
}
});


--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
 
bernd_no_junk





PostPosted: 2004-8-22 10:44:00 Top

java-programmer >> catch exceptions in EventDispatchThread Thanks for your help.

Bernd