Vibha Sanskrityayan
Vibha Sanskrityayan

Reputation: 1985

Java Compile-time error and multiple catch blocks in exception handling

Given a piece java code:

class SampleExpcetion {
  public static void main(String args[]){
        try {
                 int a[]= new int[15];
                 a[5]= 30/0;
         } 
         catch(Exception e) {System.out.println("task completed");}  
         catch(ArithmeticException e) {System.out.println("task1 completed");}  
         catch(ArrayIndexOutOfBoundsException e) { System.out.println("task2 completed");}

         System.out.println("Rest of the code......");
   }
}  

Why this piece of code will give compile-time error?

Upvotes: 1

Views: 3065

Answers (7)

Vinay Yadav
Vinay Yadav

Reputation: 1306

Exception class is the parent of ArithmeticException and ArrayIndexOutOfBoundsException and other exceptions. So, if an exception occurs and there is a catch present with Exception then, this is definitely going to catch it. And hence, other catch blocks which are written below it can't be reached, they are simply useless. So, it will become unreachable code and as we know that JAVA gives an error when some statements are unreachable. Hence, it gives Compile time error.

In case, if you would have written it after the child exceptions then it would have compiled without any error.

Upvotes: 0

user7979806
user7979806

Reputation: 1

It is so simple...The exception which is occurred here is divide by zero exception which is come under Arithmetic Exception.So it must be catched by the "arithmetic exception" catch block. But here it is catched by catch(exception e)... so The catch must be ordered like this to avoid compile-time error:

catch(ArithmeticException e) {System.out.println("task1 completed");}
catch(ArrayIndexOutOfBoundsException e) { System.out.println("task2 completed");} catch(Exception e) {System.out.println("task completed");}

Upvotes: 0

user3401570
user3401570

Reputation:

Because "Exception e" is capable of catching all the exception objects,hence other catch blocks will not be reached.This is known as "unreachable code". Unreachable code is an error in Java language.

For eg:-If Clothes is an exception class and has subclasses->Shirt and Trouser. If an exception of Shirt class has occured but we are catching that with Clothes before Shirt,then it will not reach ever to next catch block with Shirt Exception as it has been handled. This leads to the next catch block to be unreachable.

Hope it helps!!!

Upvotes: 1

dShringi
dShringi

Reputation: 1497

In a try catch block all the subsequent catch blocks shouldn't have any subclass exception in catch block. That would lead to dead code.

Also to add, in SE 7 you can specify all the types of exception that you want to check, something like:

catch (IOException|SQLException ex) {
    //...
}

Are you trying to understand what is NullPointerException ?

Upvotes: 1

Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

Because catch(Exception e) {System.out.println("task completed");} would catch all exceptions.

 catch(ArithmeticException e) {System.out.println("task1 completed");}  
 catch(ArrayIndexOutOfBoundsException e) { System.out.println("task2 completed");}

is a dead code.

Reorder like this or even better eliminate : catch(Exception e)

  catch(ArithmeticException e) {System.out.println("task1 completed");}  
  catch(ArrayIndexOutOfBoundsException e) { System.out.println("task2 completed");}
  catch(Exception e) {System.out.println("task completed");}  

Upvotes: 2

snieke
snieke

Reputation: 300

The first catch block catches all exceptions, so the others can never be reached.

Upvotes: 2

akaIDIOT
akaIDIOT

Reputation: 9231

The compiler sees that catching an ArithmeticException after its superclass is not possible; the first catch clause will always match.

Upvotes: 1

Related Questions