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
The application in Fig. Fig. 13.2, which is based on Fig. Fig. 13.1, uses exception handling to process any ArithmeticExceptions and InputMistmatchExceptions that might arise. The application still prompts the user for two integers and passes them to method quotient, which calculates the quotient and returns an int result. This version of the application uses exception handling so that if the user makes a mistake, the program catches and handles (i.e., deals with) the exception—in this case, allowing the user to try to enter the input again.
|
Fig. 13.2 Handling ArithmeticExceptions and InputMismatchExceptions.
|
|
1 2 3 import java.util.InputMismatchException; 4 import java.util.Scanner; 5 6 public class DivideByZeroWithExceptionHandling 7 { 8 9 public static int quotient( int numerator, int denominator ) 10 throws ArithmeticException 11 { 12 return numerator / denominator; 13 } 14 15 public static void main( String args[] ) 16 { 17 Scanner scanner = new Scanner( System.in ); 18 boolean continueLoop = true; 19 20 do 21 { 22 try 23 { 24 System.out.print( "Please enter an integer numerator: " ); 25 int numerator = scanner.nextInt(); 26 System.out.print( "Please enter an integer denominator: " ); 27 int denominator = scanner.nextInt(); 28 29 int result = quotient( numerator, denominator ); 30 System.out.printf( "\nResult: %d / %d = %d\n", numerator, 31 denominator, result ); 32 continueLoop = false; 33 } 34 catch ( InputMismatchException inputMismatchException ) 35 { 36 System.err.printf( "\nException: %s\n", 37 inputMismatchException ); 38 scanner.nextLine(); 39 System.out.println( 40 "You must enter integers. Please try again.\n" ); 41 } 42 catch ( ArithmeticException arithmeticException ) 43 { 44 System.err.printf( "\nException: %s\n", arithmeticException ); 45 System.out.println( 46 "Zero is an invalid denominator. Please try again.\n" ); 47 } 48 } while ( continueLoop ); 49 } 50 }
Please enter an integer numerator: 100 Please enter an integer denominator: 7 Result: 100 / 7 = 14
|
Please enter an integer numerator: 100 Please enter an integer denominator: 0 Exception: java.lang.ArithmeticException: / by zero Zero is an invalid denominator. Please try again. Please enter an integer numerator: 100 Please enter an integer denominator: 7 Result: 100 / 7 = 14
|
Please enter an integer numerator: 100 Please enter an integer denominator: hello Exception: java.util.InputMismatchException You must enter integers. Please try again. Please enter an integer numerator: 100 Please enter an integer denominator: 7 Result: 100 / 7 = 14
|
|
|