How does a try statement determine which catch clause should be used to handle anexception

When an exception is thrown within the body of a try statement, the catch clauses of thetry statement are examined in the order in which they appear. The first catch clause that iscapable of handling the exception is executed. The remaining catch clauses are ignored.

Showing Answers 1 - 4 of 4 Answers

prabhakar

  • Feb 28th, 2006
 

now once the exception is  thrown the jvm searches for the catch blocks from the most specific type to the general one in order i.e from the subclasse then to the super class for all exception.so this is the order for writing the catch blocks else JVM WILL GIVE AN ERROR AS "UNREACHABLE CODE" careful!!

  Was this answer useful?  Yes

RKalaa

  • Apr 26th, 2007
 

If you are trying to catch the specific exception  ,  write that catch clause prior to the other / general clauses.Otherwise the compilation error "The Exception already been caught" will occur.

Try this code:

try {

// some code

}

catch(Exception e) {}    // General class
catch(ArithmeticException ae) {}   // Sub class of Exception class

 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions