org.bushe.swing.exception
Class AWTExceptionHandler

java.lang.Object
  extended by org.bushe.swing.exception.AWTExceptionHandler

public class AWTExceptionHandler
extends java.lang.Object

Plug this class into the AWT Thread to handle Swing Exceptions. To plug in the exception handler, add it as a JVM property like so:

 java -Dsun.awt.exception.handler=org.bushe.swing.exception.AWTExceptionHandler
 

When this property is set, this class is instantiated and called by the EDT to when exceptions occur in the AWT Thread

This class is hopefully customizable enough for most any application.

Author:
Michael Bushe michael@bushe.com
See Also:
EventDispatchThread.handleException(Throwable)

Nested Class Summary
static interface AWTExceptionHandler.AWTErrorLogger
          An implementation of this interface can be passed to the AWTExcpetionHandler to handle logging on behalf of the AWTExceptionHandler.
 
Constructor Summary
AWTExceptionHandler()
           
 
Method Summary
protected  javax.swing.JDialog createErrorDialog(java.awt.Frame f, java.lang.Throwable t)
          Creates an ErrorDialog.
static java.lang.String getErrorEmailAddress()
          This returns null by default and if it returns null, no Email button is shown in the error dialog.
protected  java.awt.Frame getFrameForMessageDialog()
          Finds the frame for hte error message.
 void handle(java.lang.Throwable t)
          Called by the AWT EventQueue to handle the exception
protected  void handleThrowableWithFrame(java.awt.Frame f, java.lang.Throwable t)
          This method is called when there is a frame.
protected  void handleThrowableWithoutFrame(java.lang.Throwable t)
          This method is called when there is no frame.
protected  void logError(java.lang.Throwable t)
          Logs the error the current implementation of the Logger.
static void setErrorEmailAddress(java.lang.String emailAddy)
          If set to a non-null value, the Email button will be shown in the error dialog that will trigger the user's email client to send an error email to the error email address.
static void setLogger(AWTExceptionHandler.AWTErrorLogger logger)
          By default the handler logs errors to system.err.
static java.lang.String stackTraceToString(java.lang.Throwable t)
          Given a throwable, it returns the stack trace as a string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AWTExceptionHandler

public AWTExceptionHandler()
Method Detail

getErrorEmailAddress

public static java.lang.String getErrorEmailAddress()
This returns null by default and if it returns null, no Email button is shown in the error dialog.

Returns:
the current email address that the Email button on the error dialog will send to.

setErrorEmailAddress

public static void setErrorEmailAddress(java.lang.String emailAddy)
If set to a non-null value, the Email button will be shown in the error dialog that will trigger the user's email client to send an error email to the error email address.

Parameters:
emailAddy - current email address that the Email button on the error dialog will send to.

setLogger

public static void setLogger(AWTExceptionHandler.AWTErrorLogger logger)
By default the handler logs errors to system.err. To log elsehwere, set an implementation of AWTErrorLogger, probably wrapping your loggin implementation.


handle

public void handle(java.lang.Throwable t)
Called by the AWT EventQueue to handle the exception

Parameters:
t - the throwable to handle

handleThrowableWithoutFrame

protected void handleThrowableWithoutFrame(java.lang.Throwable t)
This method is called when there is no frame. Typically you will log, but just prints to system.err, or the supplied AWTErrorLogger.

Parameters:
t -

handleThrowableWithFrame

protected void handleThrowableWithFrame(java.awt.Frame f,
                                        java.lang.Throwable t)
This method is called when there is a frame. It pops up the ErrorDialog with the message

Parameters:
t - the throwable to handle

logError

protected void logError(java.lang.Throwable t)
Logs the error the current implementation of the Logger.

Parameters:
t - the throwable to log.

createErrorDialog

protected javax.swing.JDialog createErrorDialog(java.awt.Frame f,
                                                java.lang.Throwable t)
Creates an ErrorDialog. Feel free to override with yout own.

Parameters:
f - the owning frame from getFrameForMessageDialog()
t - the throwable to display
Returns:
a dialog to show

getFrameForMessageDialog

protected java.awt.Frame getFrameForMessageDialog()
Finds the frame for hte error message.

Returns:
the best frame to use for placement of the error dialog

stackTraceToString

public static java.lang.String stackTraceToString(java.lang.Throwable t)
Given a throwable, it returns the stack trace as a string

Parameters:
t - the throwable to processes
Returns:
the Stack trace as it would look in t.printStackTrace()