1 public class ExceptionTest { 2 3 public int testThrow(int x) throws java.lang.Exception { 4 if (x == 0) { 5 throw new MyException(); 6 } else if (x == 1) { 7 throw new MyOtherException(); 8 } else { 9 return 1; 10 } 11 } 12 13 public int testCatch(int x) { 14 try { 15 if (x == 0) { 16 throw new MyException(); 17 } else if (x == 1) { 18 throw new MyOtherException(); 19 } else { 20 x = 1; 21 } 22 } catch (MyException exc) { 23 x = 3; 24 } catch (MyOtherException exc) { 25 x = 2; 26 } 27 return x; 28 } 29 30 public int testIncomingCatch(int x) { 31 try { 32 return testThrow(x); 33 } catch (MyException exc) { 34 return 3; 35 } catch (MyOtherException exc) { 36 return 2; 37 } catch (java.lang.Exception exc) { 38 return -1; // Should be unreachable, really. 39 } 40 } 41 42 public int testFinally(int x) throws java.lang.Exception { 43 try { 44 if (x == 0) { 45 x = 3; 46 throw new MyException(); 47 } else if (x == 1) { 48 x = 2; 49 throw new MyOtherException(); 50 } 51 x = 1; 52 } finally { 53 x += 10; 54 } 55 return x; 56 } 57 58 public int testCatchFinally(int x) { 59 try { 60 if (x == 0) { 61 throw new MyException(); 62 } else if (x == 1) { 63 throw new MyOtherException(); 64 } 65 x = 1; 66 } catch (MyException exc) { 67 x = 3; 68 } catch (MyOtherException exc) { 69 x = 2; 70 } finally { 71 x += 10; 72 } 73 return x; 74 } 75 } 76 77 class MyException extends java.lang.Exception { 78 } 79 80 class MyOtherException extends java.lang.Exception { 81 } 82 83 // vim: tabstop=4 expandtab shiftwidth=4