Can multiple catch statements be used in exceptions ?

Can multiple catch statements be used in exceptions ?

Yes! Multiple catch statement can be used in exceptions, but with some general rules.

 A single try block can have multiple catch associated with it but the each catch has an arguments according to the inheritance heirarchy of Exception. 

That means a subtype of Exception heirarchy must not be come in the catch block before the supertype of the Exception heirarchy. 

 Example:

 try{int i=3/0; //divide by zero exception}catch(Exception e){}catch(ArithematicException e){}

 The above code will result a compilation error \'unreachable code\' at the second catch statement because ArithematicException is a subtype of the Exception class. 

The above code should be like this:try{int i=3/0; //divide by zero exception}catch(ArithematicException e){}catch(Exception e){}


By:Jalees
Date:2012-01-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Java Subjective Links

Java Subjective interview questions and answers for experienced and fresher