ErrorUtilpublic 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. |
Fields Summary |
---|
static final String[] | choicesNoMoreThe button options for the ultimate (or only) Excepton | static final String[] | choicesMoreThe button options for any non-ultimate Exception | protected static DetailsDialog | detailsDialogSecondary 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 void | handle(java.lang.Throwable th)Convenience routine for use with AWT's dispatch thread. Usage:
System.setProperty("sun.awt.exception.handler",
"com.darwinsys.swingui.ErrorUtil");
//System.out.println("handle() called with " + th.getClass().getName());
showExceptions(null, th);
| public static void | showExceptions(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(
parent,
message,
className, // title
JOptionPane.YES_NO_CANCEL_OPTION, // icontType
JOptionPane.ERROR_MESSAGE, // messageType
null, // icon
choices, // options
choices[0] // default
);
if (response == 0) // "OK"
return;
if (response == 1) { // "Details"
// show a JDialog with a JTextArea of printStackTrace();
if (detailsDialog == null)
detailsDialog = new DetailsDialog((JFrame)parent);
detailsDialog.showStackTrace(theExc);
}
// else resp = 2, "Next", let it fall through:
theExc = next;
} while (next != null);
|
|