H.3. Example: Handling ArithmeticExceptions and InputMismatchExceptions

The application in Fig. H.2, which is based on Fig. H.1, uses exception handling to process any ArithmeticExceptions and InputMistmatchExceptions that 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 enter the input again.

 1   // Fig. H.2: DivideByZeroWithExceptionHandling.java 2   // Handling ArithmeticExceptions and InputMismatchExceptions. 

Get Android™ How to Program, Second 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.