Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576
Buy the Book!
Amazon logo
InformIT logo

In Part 1 of this tutorial we showed the premature termination of a program in which unhandled exceptions occur. In this tutorial, we demonstrate how to catch and handle these exceptions to enable the program to continue executing. This tutorial is intended for students and professionals who are familiar with classes and basic inheritance concepts in Java.

Download the code for this tutorial here.

[Note: This tutorial is an excerpt (Section 13.4) of Chapter 13, Exception Handling, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.643-648. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

13.4 Handling ArithmeticExceptions and InputMismatchExceptions (Continued)

Now let us examine method quotient (lines 9–13). The portion of the method declaration located at line 10 is known as a throws clause. A throws clause specifies the exceptions the method throws. This clause appears after the methods parameter list and before the method’s body. The clause contains a comma-separated list of the exceptions that the method will throw if a problem occurs. Such exceptions may be thrown by statements in the method’s body or by methods called in the body. A method can throw exceptions of the classes listed in its throws clause or of their subclasses. We have added the throws clause to this application to indicate to the rest of the program that this method may throw an ArithmeticException. Clients of method quotient are thus informed that the method may throw an ArithmeticException and that the exception should be caught. You will learn more about the throws clause in Section Section 13.6.

Error-Prevention Tip
Error-Prevention Tip 13.3
If you know that a method might throw an exception, include appropriate exception-handling code in your program to make it more robust.
Error-Prevention Tip
Error-Prevention Tip 13.4
Read the online API documentation for a method before using that method in a program. The documentation specifies the exceptions thrown by the method (if any) and indicates reasons why such exceptions may occur. Then provide for handling those exceptions in your program.
Error-Prevention Tip
Error-Prevention Tip 13.5
Read the online API documentation for an exception class before writing exception-handling code for that type of exception. The documentation for an exception class typically contains potential reasons that such exceptions occur during program execution.

Error-Prevention Tip
Error-Prevention Tip 13.3
If you know that a method might throw an exception, include appropriate exception-handling code in your program to make it more robust.
Error-Prevention Tip
Error-Prevention Tip 13.4
Read the online API documentation for a method before using that method in a program. The documentation specifies the exceptions thrown by the method (if any) and indicates reasons why such exceptions may occur. Then provide for handling those exceptions in your program.
Error-Prevention Tip
Error-Prevention Tip 13.5
Read the online API documentation for an exception class before writing exception-handling code for that type of exception. The documentation for an exception class typically contains potential reasons that such exceptions occur during program execution.

     When line 12 executes, if the denominator is zero, the JVM throws an ArithmethicException object. This object will be caught by the catch block at lines 42–47, which displays basic information about the exception by implicitly invoking the exception’s toString method, then asks the user to try again. If the denominator is not zero, method quotient performs the division and returns the result to the point of invocation of method quotient in the try block (line 29). Lines 30–31 display the result of the calculation and line 32 sets continueLoop to false. In this case, the try block completes successfully, so the program skips the catch blocks, fails the condition at line 48 and method main completes execution normally. Note that when quotient throws an ArithmeticException, quotient terminates and does not return a value, and quotient’s local variables go out of scope (and the variables are destroyed). If quotient contained local variables that were references to objects and there are no other references to those object, would be marked for garbage collection. Also, when an exception occurs, the try block from which quotient was called terminates before lines 30–32 can execute. Here, too, if local variables were created in the try block prior to the exception being thrown, these variables would go out of scope. If an InputMismatchException is generated by lines 25 or 27, the try block terminates and execution continues with the catch block at lines 34–41. In this case, method quotient is not called. Then method main continues after the last catch block (line 48).

Page 1 | 2 | 3 | 4
Related Tutorials :
Divide By Zero Without Exception Handling
Return to Tutorial Index
Google