| @@ -0,0 +1,59 @@ |
| | 1 | |
| | 2 | import static utils.Utils.*; |
| | 3 | |
| | 4 | public class ExceptionsTest { |
| | 5 | |
| | 6 | public static void strictFunction(Integer arg) { |
| | 7 | try { |
| | 8 | int number = arg; |
| | 9 | if (number < 0) |
| | 10 | throw new IllegalArgumentException("we do not accept negatives"); |
| | 11 | System.out.println("Whee, strictFunction call is complete!"); |
| | 12 | } |
| | 13 | catch (NullPointerException e) { |
| | 14 | System.out.println("Null pointer exception caught: " + e.getMessage()); |
| | 15 | } |
| | 16 | catch (IllegalArgumentException e) { |
| | 17 | System.out.println("Illegal argument exception caught: " + e.getMessage()); |
| | 18 | if (arg == -42) |
| | 19 | throw new RuntimeException("the negative answer is awful"); |
| | 20 | } |
| | 21 | finally { |
| | 22 | System.out.println("strictFunction finally"); |
| | 23 | if (arg != null && arg == 42) |
| | 24 | throw new RuntimeException("the answer is not accepted"); |
| | 25 | } |
| | 26 | } |
| | 27 | |
| | 28 | public static void main(String[] args) { |
| | 29 | System.out.println("Simple test"); |
| | 30 | System.out.println("Before exception"); |
| | 31 | try { |
| | 32 | System.out.println("In try block"); |
| | 33 | throw new IllegalArgumentException(); |
| | 34 | } |
| | 35 | catch (IllegalArgumentException e) { |
| | 36 | System.out.println("Exception caught"); |
| | 37 | } |
| | 38 | finally { |
| | 39 | System.out.println("Clean up"); |
| | 40 | } |
| | 41 | System.out.println("After exception"); |
| | 42 | |
| | 43 | System.out.println("Extendend test"); |
| | 44 | strictFunction(null); |
| | 45 | strictFunction(-1); |
| | 46 | try { |
| | 47 | strictFunction(42); |
| | 48 | } |
| | 49 | catch (RuntimeException e) { |
| | 50 | System.out.println("Runtime exception caught: " + e.getMessage()); |
| | 51 | } |
| | 52 | try { |
| | 53 | strictFunction(-42); |
| | 54 | } |
| | 55 | catch (RuntimeException e) { |
| | 56 | System.out.println("Runtime exception caught: " + e.getMessage()); |
| | 57 | } |
| | 58 | } |
| | 59 | } |