diff options
Diffstat (limited to 'java/11-error-handling-2')
| -rw-r--r-- | java/11-error-handling-2/.gitignore | 2 | ||||
| -rw-r--r-- | java/11-error-handling-2/Maths.java | 11 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsException.java | 22 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest.java | 32 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest2.java | 27 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest3.java | 21 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest4.java | 33 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest5.java | 21 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest6.java | 22 | ||||
| -rw-r--r-- | java/11-error-handling-2/MathsTest7.java | 22 |
10 files changed, 213 insertions, 0 deletions
diff --git a/java/11-error-handling-2/.gitignore b/java/11-error-handling-2/.gitignore new file mode 100644 index 0000000..e493dd6 --- /dev/null +++ b/java/11-error-handling-2/.gitignore @@ -0,0 +1,2 @@ +bin +dist diff --git a/java/11-error-handling-2/Maths.java b/java/11-error-handling-2/Maths.java new file mode 100644 index 0000000..b61a9f2 --- /dev/null +++ b/java/11-error-handling-2/Maths.java @@ -0,0 +1,11 @@ +public class Maths { + + public int factorial(int number) throws MathsException { + if (number < 0) { + MathsException e = new MathsException("Factorial of " + number + " not defined"); +// e.setX(100); + throw e; + } + return number == 0 ? 1 : number * factorial(number - 1); + } +} diff --git a/java/11-error-handling-2/MathsException.java b/java/11-error-handling-2/MathsException.java new file mode 100644 index 0000000..bdbaf81 --- /dev/null +++ b/java/11-error-handling-2/MathsException.java @@ -0,0 +1,22 @@ +public class MathsException extends Exception { + + public MathsException() { + // TODO Auto-generated constructor stub + } + + public MathsException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public MathsException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public MathsException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/java/11-error-handling-2/MathsTest.java b/java/11-error-handling-2/MathsTest.java new file mode 100644 index 0000000..7b6827c --- /dev/null +++ b/java/11-error-handling-2/MathsTest.java @@ -0,0 +1,32 @@ +public class MathsTest { + + /** + * @param args + */ + public static void main(String[] args) { + Maths m = new Maths(); + int result = 0; + + try { + m.factorial(-5); + m.factorial(5); // Not executed + } catch (MathsException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + try { + result = m.factorial(-5); + // ... + } catch(MathsException me) { +// System.err.println(me.getX()); + System.err.println("Factorial method failed"); + } finally { + System.out.println("Inside finally block"); + } + + System.out.println(result); + + } + +} diff --git a/java/11-error-handling-2/MathsTest2.java b/java/11-error-handling-2/MathsTest2.java new file mode 100644 index 0000000..4aa8b59 --- /dev/null +++ b/java/11-error-handling-2/MathsTest2.java @@ -0,0 +1,27 @@ +public class MathsTest2 { + + /** + * @param args + */ + public static void main(String[] args) { + Maths m = new Maths(); + int result = 0; + + try { + result = m.factorial(-5); + } catch(MathsException me) { // Catching a specific exception + System.err.println("Caught a MathsException"); + } catch(Exception e) { // Catching the super type + System.err.println("Caught an Exception"); + } + + try { + result = m.factorial(-5); + } catch (MathsException | ArrayIndexOutOfBoundsException e) { // Java SE 7 + + } + + System.out.println(result); + } + +} diff --git a/java/11-error-handling-2/MathsTest3.java b/java/11-error-handling-2/MathsTest3.java new file mode 100644 index 0000000..ca45e5c --- /dev/null +++ b/java/11-error-handling-2/MathsTest3.java @@ -0,0 +1,21 @@ +public class MathsTest3 { + + /** + * @param args + */ + public static void main(String[] args) { + Maths m = new Maths(); + int result = 0; + + try { + result = m.factorial(-5); + } catch(MathsException e) { + System.err.println("Factorial method failed"); + System.err.println(e.getMessage()); + e.printStackTrace(); + } + + System.out.println(result); + } + +} diff --git a/java/11-error-handling-2/MathsTest4.java b/java/11-error-handling-2/MathsTest4.java new file mode 100644 index 0000000..b5cca8a --- /dev/null +++ b/java/11-error-handling-2/MathsTest4.java @@ -0,0 +1,33 @@ +public class MathsTest4 { + + /** + * @param args + * @throws MathsException + */ + public static void main(String[] args) throws MathsException { + Maths m = new Maths(); + int result = 0; + + // No catching! Then the method should be declared + // to throw the exception (or a super type like + // Exception or even Throwable) + +// m.factorial(5); + + // Look! A try block can be defined without a catch block + // as well. + try { + result = m.factorial(-5); + System.out.println("xxx"); + } finally { + System.out.println("Inside finally block"); + } + + // No try block is also possible + // Since the method is declared to duct the exception + result = m.factorial(-5); + + System.out.println(result); + } + +} diff --git a/java/11-error-handling-2/MathsTest5.java b/java/11-error-handling-2/MathsTest5.java new file mode 100644 index 0000000..56bf64d --- /dev/null +++ b/java/11-error-handling-2/MathsTest5.java @@ -0,0 +1,21 @@ +public class MathsTest5 { + + /** + * @param args + */ + public static void main(String[] args) throws Exception { + Maths m = new Maths(); + int result = 0; + + try { + result = m.factorial(-5); + } catch (MathsException me) { + throw new Exception("My exception wrapper", me); + } finally { + System.out.println("Inside finally block"); + } + + System.out.println(result); + } + +} diff --git a/java/11-error-handling-2/MathsTest6.java b/java/11-error-handling-2/MathsTest6.java new file mode 100644 index 0000000..7f176aa --- /dev/null +++ b/java/11-error-handling-2/MathsTest6.java @@ -0,0 +1,22 @@ +public class MathsTest6 { + + /** + * @param args + */ + public static void main(String[] args) { + Maths m = new Maths(); + int result = 0; + + try { + result = m.factorial(-5); + } catch(MathsException me) { + System.err.println("Factorial method failed"); + System.exit(1); + } finally { + System.out.println("Inside finally block"); + } + + System.out.println(result); + } + +} diff --git a/java/11-error-handling-2/MathsTest7.java b/java/11-error-handling-2/MathsTest7.java new file mode 100644 index 0000000..f55cfad --- /dev/null +++ b/java/11-error-handling-2/MathsTest7.java @@ -0,0 +1,22 @@ +public class MathsTest7 { + + /** + * @param args + */ + public static void main(String[] args) { + Maths m = new Maths(); + int result = 0; + + try { + result = m.factorial(-5); + } catch(MathsException me) { + System.err.println("Factorial method failed"); + return; + } finally { + System.out.println("Inside finally block"); + } + + System.out.println(result); + } + +} |
