Hosting your own web site - Chapter 14 Exception Handling 817 When the user
Chapter 14 Exception Handling 817 When the user inputs the denominator and presses the Enter key, the program calls method actionPerformed (lines 54 84). Next, method actionPerformed proceeds with a try block (lines 61 67), which encloses the code that may throw an exception and any code that should not be executed if an exception occurs. The statements that read the integers from the JTextFields (lines 62 63) each use method Integer.parseInt to convert Strings to int values. Method parseInt throws a NumberFormatException if its String argument is not a valid integer. The division that can cause the divide-by-zero error is not performed explicitly in the try block. Rather, the call to method quotient (line 65) invokes the code that attempts the division. Method quotient (lines 89 96) throws the DivideByZeroException object, as we will see momentarily. In general, errors may surface through explicitly mentioned code in a tryblock, through calls to a method or even through deeply nested method calls initiated by code in a try block. The tryblock in this example is followed by two catch blocks lines 70 75 contain the exception handler for the NumberFormatException and lines 78 83 contain the exception handler for the DivideByZeroException. In general, when the program detects an exception while executing a try block, the program catches the exception in a catch block that specifies an appropriate exception type (i.e., the type in the catch matches the thrown exception type exactly or is a superclass of the thrown exception type). In Fig. 14.2, the first catch block specifies that it will catch exception objects of type NumberFormatException (this type matches the exception object type thrown in method Integer.parseInt) and the second catch block specifies that it will catch exception objects of type ArithmeticException (this type is a superclass of the exception object type thrown in method quotient). Only the matching catch handler executes when an exception occurs. Both our exception handlers simply display an error- message dialog, but exception handlers can be more elaborate than this. After executing an exception handler, program control proceeds to the first statement after the last catch block (or in the finally block, if one is present). If the code in the tryblock does not throw an exception, then the catch handlers are skipped and execution resumes with the first line of code after the catch handlers (or in the finally block, if one is present). In Fig. 14.2, method actionPerformed simply returns, but the program could continue executing more statements after the catch blocks. Testing and Debugging Tip 14.5 With exception handling, a program can continue executing after dealing with a problem. This helps ensure robust applications that contribute to what is called mission-critical computing or business-critical computing. Now let us examine method quotient(lines 89 96). When the if structure determines that denominator is zero, the body of the if executes a throw statement that creates and throws a new DivideByZeroException object. This object will be caught by the catch block (lines 78 83) specifying type ArithmeticException after the try block. The catch block specifies parameter name arithmeticException to receive the thrown exception object. The ArithmeticException handler converts the exception to a Stringvia toString and passes this String as the message to display in an error-message dialog. If denominator is not zero, quotient does not throw an exception. Rather, quotient performs the division and returns the result of the division to the point of invocation
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.