Try with multiple catch blocks
Whenever we perform multiple tasks in a try block and if that try block might throw a different type of exceptions then we use java multi-catch blocks with a single try block.
The order of catch block is very important and that must be written from child exception class to parent exception class.
package com.exceptions;
public class Test {
public static void main(String[] args) {
try {
int[] arr=new int[5];
arr[5]=50;
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("pass the value in correct index " +e);
}
catch (Exception e) {
System.out.println("handle exception "+e);
}
}
}
In the above program, in the try block, JVM throws an exception that we handled by two catch blocks. If it is ArrayIndexOutOfBoundsException then first catch block handle that exception, if not second catch block handle.
pass the value in correct index java.lang.ArrayIndexOutOfBoundsException: 5
If you notice that catch blocks, the order is from child exception to parent exception class.
Case-1: Exception handling flow
In the same case, try block throw ArrayIndexOutOfBoundsException and it handled by second catch block because Exception is parent class and the first catch block is handled only ArithmeticException.
package com.exceptions;
public class Test {
public static void main(String[] args) {
try {
int[] arr=new int[5];
arr[5]=50;
}
catch (ArithmeticException e) {
System.out.println("pass the value in correct index " +e);
}
catch (Exception e) {
System.out.println("handle exception "+e);
}
}
}
This program will give the output as
handle exception java.lang.ArrayIndexOutOfBoundsException: 5
Case-2: Order Of catch block
The order of catch blocks is must, all the catch blocks must be in order from child to parent exception class. If order is not matched then it will give compile time error.
package com.exceptions;
public class Test {
public static void main(String[] args) {
try {
int[] arr=new int[5];
arr[5]=50;
}
catch (Exception e) {
System.out.println("handle exception "+e);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("pass the value in correct index " +e);
}
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for ArrayIndexOutOfBoundsException. It is already handled by the catch block for Exception
at com.exceptions.Test.main(Test.java:11)
Case-3: One Exception occur and one catch block execute
At a time only one exception occurs and one catch block execute
package com.exceptions;
public class Test {
public static void main(String[] args) {
try {
int i=10/0;//ArithmeticException
int[] arr=new int[5];
arr[5]=50;//ArrayIndexOutOfBoundsException
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("pass the value in correct index " +e);
}
catch (ArithmeticException e) {
System.out.println("don't divide by zero " +e);
}
catch (Exception e) {
System.out.println("handle exception "+e);
}
}
}
In this program, try block has two exceptions first one is ArithmeticException so first ArithmeticException throws by JVM and handled by its corresponding catch block.
It gives the output as
don't divide by zero java.lang.ArithmeticException: / by zero
No comments:
Post a Comment