public class TwoCatchesDemo
1: //TwoCatchesDemo.java
2:
3: import java.util.Scanner;
4:
5: public class TwoCatchesDemo
6: {
7: public static void main(String[] args)
8: {
9: try
10: {
11: System.out.println("Enter number of widgets produced:");
12: Scanner keyboard = new Scanner(System.in);
13: int widgets = keyboard.nextInt();
14: if (widgets < 0)
15: throw new NegativeNumberException("widgets");
16:
17: System.out.println("How many were defective?");
18: int defective = keyboard.nextInt();
19: if (defective < 0)
20: throw new NegativeNumberException("defective widgets");
21:
22: double ratio = exceptionalDivision(widgets, defective);
23: System.out.println("One in every " + ratio
24: + " widgets is defective.");
25: }
26: catch (DivideByZeroException e)
27: {
28: System.out.println("Congratulations! A perfect record!");
29: }
30: catch (NegativeNumberException e)
31: {
32: System.out.println("Cannot have a negative number of "
33: + e.getMessage());
34: }
35: System.out.println("End of program.");
36: }
37:
38: public static double exceptionalDivision
39: (
40: double numerator,
41: double denominator
42: ) throws DivideByZeroException
43: {
44: if (denominator == 0)
45: throw new DivideByZeroException();
46: return numerator / denominator;
47: }
48: }