13.3. Example: Divide by Zero without Exception Handling

First we demonstrate what happens when errors arise in an application that does not use exception handling. Figure 13.1 prompts the user for two integers and passes them to method quotient, which calculates the quotient and returns an int result. In this example, we’ll see that exceptions are thrown (i.e., the exception occurs) when a method detects a problem and is unable to handle it.

Figure 13.1. Integer division without exception handling.
 1 // Fig. 13.1: DivideByZeroNoExceptionHandling.java
 2 // An application that attempts to divide by zero.
 3 import java.util.Scanner;
 4
 5 public class DivideByZeroNoExceptionHandling
 6 {
 7  // demonstrates throwing an exception when a divide-by-zero ...

Get Java™ How to Program, Seventh Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.