ErrorUtil.javaAPI DocExample4019Sun Mar 07 11:14:58 GMT 2004com.darwinsys.swingui


public class ErrorUtil extends Object
Convenience class for fielding Exceptions in a Swing App. Displays exceptions in a JOptionPane, and follows chained exceptions, both the 1.x SQLException.getNextExeption() and the new 1.4 Exception.getCause().

The user (will be able to) press a Details... button to see the traceback in a dialog; tracebacks are not displayed unless the user requests them.

Ian Darwin
$Id:,v 1.8 2004/03/07 17:14:57 ian Exp $

Fields Summary
static final String[]
The button options for the ultimate (or only) Excepton
static final String[]
The button options for any non-ultimate Exception
protected static DetailsDialog
Secondary dialog for the "Details..." button
Constructors Summary
public ErrorUtil()
Public no-arg constructor for those who like simple instantiation.

		// Nothing to do
Methods Summary
public voidhandle(java.lang.Throwable th)
Convenience routine for use with AWT's dispatch thread. Usage:

		//System.out.println("handle() called with " + th.getClass().getName());
		showExceptions(null, th);
public static voidshowExceptions(java.awt.Component parent, java.lang.Throwable theExc)
Show the given Exception (and any nested Exceptions) in JOptionPane(s).

		Throwable next = null;

		do {
			String className = theExc.getClass().getName();
			String message = className;

			if (theExc instanceof SQLException) {
				SQLException sexc = (SQLException)theExc;
				message += "; code=" + sexc.getErrorCode();
				next = sexc.getNextException();
			// else next = theExc.getCause();   // Comment out if < JDK 1.4

			String[] choices = next != null ? choicesMore : choicesNoMore;

			/* Show the Dialog! */
			int response = JOptionPane.showOptionDialog(
				className,	 						// title
				JOptionPane.YES_NO_CANCEL_OPTION,	// icontType
				JOptionPane.ERROR_MESSAGE,			// messageType
				null,								// icon
				choices,							// options
				choices[0]							// default

			if (response == 0)			// "OK"
			if (response == 1) {		// "Details"
				// show a JDialog with a JTextArea of printStackTrace();
				if (detailsDialog == null)
					detailsDialog = new DetailsDialog((JFrame)parent);
			// else resp = 2, "Next", let it fall through:

			theExc = next;

		} while (next != null);