Java Tutorial
class ExceptionTest { static int division() { int divider = 0; int result = 100/divider; return result; } public static void main(String args[]) { int val = ExceptionTest.division(); System.out.println(val); } }Java code raises ArithmeticException here.
$ javac ExceptionTest.java $ java ExceptionTest Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionTest.division(ExceptionTest.java:4) at ExceptionTest.main(ExceptionTest.java:8)
try { //statements to monitor exception's } catch(ExceptionType1 exceptionObject) { //exception handler statements for ExceptionType1 } catch(ExceptionType2 exceptionObject) { //exception handler statements for ExceptionType2 } //... finally { //Statements to be executed before try statements end }Example try catch:
class ExceptionHandlingTest { static int division() { int divider = 0; int result = 100/divider; return result; } public static void main(String args[]) { int val = 0; try { val = ExceptionTest.division(); System.out.println(val); } catch(ArithmeticException ex) { System.out.println("Division by zero error is occurred."); } } }Output:
$ javac ExceptionHandlingTest.java $ java ExceptionHandlingTest Division by zero error is occurred.
//Multiple catch with finally blocks class MultipleCatchFinallyTest { public static void main(String args[]) { int val = 0; try { int d =0; val = 100/d; int a[] = new int[2]; a[3] = 100; System.out.println(val); } catch(ArithmeticException ex) { System.out.println("Division by zero error is occurred."); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Array index out of bound error is occurred."); } finally { System.out.println("After try and catch block!"); } } }Output:
Division by zero error is occurred. After try and catch block!
class NestedTryCatchTest { public static void main(String args[]) { int val = 0; try { int d =0; try { val = 100/d; } catch(ArithmeticException ex) { System.out.println("Division by zero error is occurred."); } int a[] = new int[2]; a[3] = 100; System.out.println(val); } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Array index out of bound error is occurred."); } finally { System.out.println("After try and catch block!"); } } }Output:
$ javac NestedTryCatchTest.java $ java NestedTryCatchTest Division by zero error is occurred. Array index out of bound error is occurred. After try and catch block!
class ThrowTest { static int division() { int divider = 0, result; try { result = 100/divider; } catch(ArithmeticException ex) { System.out.println("division method: Division by zero error."); // rethrow the same exception to calling method throw ex; } return result; } public static void main(String args[]) { int val = 0; try { val = ThrowTest.division(); System.out.println(val); } catch(ArithmeticException ex) { System.out.println("Recaught: " + ex); } } }Output:
$ javac ThrowTest.java $ java ThrowTest division method: Division by zero error. Recaught: java.lang.ArithmeticException: / by zero
type method-name(parameter-list) throws exception-list { // method definition }throws example:
class ThrowsTest { static int division() throws ArithmeticException { int divider = 0, result; result = 100/divider; return result; } public static void main(String args[]) { int val = 0; try { val = ThrowTest.division(); System.out.println(val); } catch(ArithmeticException ex) { System.out.println("Exception: " + ex); } } }Output:
$ javac ThrowsTest.java $ java ThrowsTest Exception: java.lang.ArithmeticException: / by zero
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page