paul@16 | 1 | public class ExceptionTest { |
paul@16 | 2 | |
paul@16 | 3 | public int testCatch() { |
paul@16 | 4 | try { |
paul@16 | 5 | throw new MyException(); |
paul@16 | 6 | } catch (MyException exc) { |
paul@16 | 7 | return 1; |
paul@16 | 8 | } |
paul@16 | 9 | } |
paul@16 | 10 | |
paul@19 | 11 | public int testFinally(int x) throws java.lang.Exception { |
paul@19 | 12 | try { |
paul@19 | 13 | if (x == 0) { |
paul@19 | 14 | throw new MyException(); |
paul@19 | 15 | } else if (x == 1) { |
paul@19 | 16 | throw new MyOtherException(); |
paul@19 | 17 | } |
paul@19 | 18 | } finally { |
paul@19 | 19 | x = 1; |
paul@19 | 20 | } |
paul@19 | 21 | return x; |
paul@19 | 22 | } |
paul@19 | 23 | |
paul@19 | 24 | public int testCatchFinally(int x) { |
paul@16 | 25 | try { |
paul@16 | 26 | if (x == 0) { |
paul@16 | 27 | throw new MyException(); |
paul@16 | 28 | } else if (x == 1) { |
paul@16 | 29 | throw new MyOtherException(); |
paul@16 | 30 | } |
paul@16 | 31 | } catch (MyException exc) { |
paul@16 | 32 | x = 3; |
paul@16 | 33 | } catch (MyOtherException exc) { |
paul@16 | 34 | x = 2; |
paul@16 | 35 | } finally { |
paul@16 | 36 | x = 1; |
paul@16 | 37 | } |
paul@16 | 38 | return x; |
paul@16 | 39 | } |
paul@16 | 40 | } |
paul@16 | 41 | |
paul@16 | 42 | class MyException extends java.lang.Exception { |
paul@16 | 43 | } |
paul@16 | 44 | |
paul@16 | 45 | class MyOtherException extends java.lang.Exception { |
paul@16 | 46 | } |
paul@16 | 47 | |
paul@16 | 48 | // vim: tabstop=4 expandtab shiftwidth=4 |