class ComputeQuotientWithException1
1: //ComputeQuotientWithException1.java:Adapted from Holmes, Chapter 9
2: //Demonstrates creating an arithmetic exception and "catching" it.
4: import java.io.*;
6: class ComputeQuotientWithException1
7: {
8: static BufferedReader keyboard =
9: new BufferedReader(new InputStreamReader(System.in));
10: static PrintWriter screen =
11: new PrintWriter(System.out, true);
13: public static void main(String[] args)
14: throws Exception
15: {
16: int dividend, divisor, quotient;
17: boolean done = false;
19: do
20: {
21: try
22: {
23: screen.print("\nEnter dividend: ");
24: screen.flush();
25: dividend =
26: new Integer(keyboard.readLine()).intValue();
28: screen.print("Enter divisor: ");
29: screen.flush();
30: divisor =
31: new Integer(keyboard.readLine()).intValue();
33: //Arithmetic exception thrown here
34: //if divisor is zero
35: quotient = dividend/divisor;
36: screen.println("Quotient = " + quotient + "\n");
37: done = true;
38: }
39: catch (ArithmeticException arithException)
40: {
41: screen.println("Exception " +
42: arithException.toString() +
43: " caught.\nPlease try again ...");
44: }
45: }
46: while (!done);
47: }
48: }