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 ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.