Java Multi catch block
If you have to perform different tasks at the occurrence of different Exceptions, use java multi catch block.
Let's see a simple example of java multi-catch block.
- public class TestMultipleCatchBlock{
- public static void main(String args[]){
- try{
- int a[]=new int[5];
- a[5]=30/0;
- }
- catch(ArithmeticException e){System.out.println("task1 is completed");}
- catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
- catch(Exception e){System.out.println("common task completed");}
- System.out.println("rest of the code...");
- }
- }
Output:task1 completed
rest of the code..
Another Example :
class TestMultipleCatchBlock1{
public static void main(String args[]){
Output : Compile-time error
- try{
- int a[]=new int[5];
- a[5]=30/0;
- }
- catch(Exception e){System.out.println("common task completed");}
- catch(ArithmeticException e){System.out.println("task1 is completed");}
- catch(ArrayIndexOutOfBoundsException e){System.out.println("task 2 completed");}
- System.out.println("rest of the code...");
- }
- }
No comments:
Post a Comment