Flow Chart of Multi Catch Block
public class Main { public static void main(String args[]){ try{ // try block int Num1 =10/0; int arr[]=new int[10]; arr[5]=15; } catch(ArithmeticException e){ System.out.println(e); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } System.out.println("This Code after outer catch block"); } }
Output:
java.lang.ArithmeticException: / by zero
This Code after outer catch block
public class Main { public static void main(String args[]){ try{ // try block int arr[]=new int[10]; arr[15]=15; } catch(ArithmeticException e){ System.out.println(e); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } System.out.println("This Code after outer catch block"); } }
Output:
java.lang.ArrayIndexOutOfBoundsException: Index 15 out of bounds for length 10
This Code after outer catch block
The try block in the example below has two exceptions. However, only one exception occurs at a time, and its associated catch block is executed.
public class Main { public static void main(String args[]){ try{ // try block int arr[]=new int[10]; arr[15]=15/0; System.out.println(arr[15]); } catch(ArithmeticException e){ System.out.println(e); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } System.out.println("This Code after outer catch block"); } }
Output:
java.lang.ArithmeticException: / by zero
This Code after outer catch block
Post your comment