The Java Program: Except.java
1
2
3 class MyException extends Exception {
4 public MyException (String s) {super (s);};
5 }
6 class MySubException extends MyException {
7 public MySubException (String s) {super (s);};
8 }
9 class MyOtherException extends Exception {
10 public MyOtherException (String s) {super (s);};
11 }
12
13 public class Except {
14
15 public static void main (String argv[]) {
16 int i;
17 try {
18 i = Integer.parseInt (argv[0]);
19 } catch (ArrayIndexOutOfBoundsException e) {
20 System.out.println ("An argument is required.");
21 return;
22 } catch (NumberFormatException e) {
23 System.out.println ("The argument must be an integer.");
24 return;
25 }
26
27 a (i);
28 }
29
30 public static void a (int i) {
31 try {
32 b (i);
33 } catch (MySubException e) {
34 System.out.print ("MySubException: " + e.getMessage() + ".");
35 System.out.println (" Caught in handler 1.");
36 } catch (MyException e) {
37 System.out.print ("MyException: " + e.getMessage() + ".");
38 System.out.println (" Caught in handler 2.");
39 }
40 }
41
42 public static void b (int i) throws MyException {
43 int result;
44 try {
45 System.out.print ("i="+i);
46 result = c(i);
47 System.out.print (" c(i)="+result);
48 } catch (MyOtherException e) {
49 System.out.println ();
50 System.out.print ("MyOtherException: " + e.getMessage() + ".");
51 System.out.print (" Caught in handler 3.");
52 } finally {
53 System.out.println ();
54 }
55 }
56
57 public static int c (int i) throws MyException, MyOtherException {
58 switch (i) {
59 case 0:
60 throw new MyException ("input too low");
61 case 1:
62 throw new MySubException ("input still too low");
63 case 9:
64 throw new MyOtherException ("input too high");
65 default:
66 return (i*i);
67 }
68
69 }
70
71 }