Error events

In most cases, the IBM Toolbox for Java graphical user interface (GUI) components fire error events instead of throw exceptions.

An error event is a wrapper around an exception that is thrown by an internal component.

You can provide an error listener that handles all error events that are fired by a particular graphical user interface component. Whenever an exception is thrown, the listener is called, and it can provide appropriate error reporting. By default, no action takes place when error events are fired.

The IBM Toolbox for Java provides a graphical user interface component called ErrorDialogAdapter, which automatically displays a dialog to the user whenever an error event is fired.

Examples

The following examples show how you can handle errors and define a simple error listener.

Example: Handling error events by displaying a dialog

The following example shows how you can handle error events by displaying a dialog:

                       // ... all the setup work to lay out
                       // a graphical user interface
                       // component is done. Now add an
                       // ErrorDialogAdapter as a listener
                       // to the component. This will report
                       // all error events fired by that
                       // component through displaying a
                       // dialog.
     ErrorDialogAdapter errorHandler = new ErrorDialogAdapter (parentFrame);
     component.addErrorListener (errorHandler);

You can write a custom error listener to handle errors in a different way. Use the ErrorListener interface to accomplish this.

Example: Defining an error listener

The following example shows how to define an simple error listener that only prints errors to System.out:

     class MyErrorHandler
     implements ErrorListener
     {
                       // This method is invoked whenever
                       // an error event is fired.
             public void errorOccurred(ErrorEvent event)
             {
                  Exception e = event.getException ();
                  System.out.println ("Error: " + e.getMessage ());
             }
     } 

Example: Handling error events by using an error listener

The following example shows how to handle error events for a graphical user interface component using this customized handler:

     MyErrorHandler errorHandler = new MyErrorHandler ();
     component.addErrorListener (errorHandler);