O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

An Error Handler Dialog

Example 11-18 is another example of a custom dialog. This dialog is designed for use when an unrecoverable or unexpected exception occurs in a program. Instead of just printing a stack trace and exiting, as Java programs do by default, the static method ErrorHandler.displayThrowable( ) displays the exception detail message to the user. One of the buttons in the dialog allows the user to view the complete stack trace for the exception, and another button allows the user to report the exception to the program's developer (by POSTing a serialized copy of the exception object to a web server).

One of the interesting features of this example is that the dialog can change its layout and size. As illustrated in Figure 11-15, the dialog includes a Show Details button that expands the display to include a stack trace. Similarly, if the Send Report button is clicked, the dialog changes to display the text sent by the web server in response to the error report.

An ErrorHandler dialog, in three different states

Figure 11-15. An ErrorHandler dialog, in three different states

The displayThrowable( ) method is assisted by two other static convenience methods that may also be useful on their own. First, getHTMLDetails( ) returns an HTML-formatted string describing the stack trace of a Throwable object. It illustrates two methods of Throwable that are new in Java 1.4. getStackTrace( ) returns an array of StackTraceElement ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required