aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exceptions" Watch "exceptions" New topic
Author

exceptions

Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
One of dan chisolm exam question answers

A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block.


But I tried this code and it seems to work fine



SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I tried this with a method that throws IOException. I did a catch on the IOException followed by a catch on the Exception. This compiled and ran fine.

It might help to know the specific throw and catch clauses for the problem.

I know that order of Exceptions is important and enforced by the compiler.

I know that if you try to catch something that is not thrown, and is not a superclass or subclass of what is thrown, you will get a compiler error.

example:


This will show an error because a SQLException cannot be thrown.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I tried this code and it seems to work fine:

try{
}catch (IllegalArgumentException e){
}
catch(Exception e){
}


IllegalArgumentException is an unchecked exception. javac does not keep track of where it might be thrown. So the rule that a catch block for an exception that will never be thrown is an unreachable statement is unenforcable for unchecked exceptions.


Mike Gershman
SCJP 1.4, SCWCD in process
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
This code actually does not produce any compile-time error:

try{ }
catch (IllegalArgumentException e){}
catch(Exception e){}

However, if write it this way it does:

try{ }
catch(Exception e){}
catch (IllegalArgumentException e){}

Because any IllegalArgumentException will be caught by the Exception catch block since this is a super class of IllegalArgumentException.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
any IllegalArgumentException will be caught by the Exception catch block since this is a super class of IllegalArgumentException.

Yes, there is the rule that a catch clause should not be completely shadowed by a preceding catch clause.

There is also a rule against unreachable statements. One such case is a catch clause specifying an exception that will never be thrown in the associated try block. javac enforces this for catch clauses specifying checked exceptions like IOException and InterruptedException. However, javac does not verify that unchecked exceptions like IllegalArgumentException or mixed exceptions like Exception can actually be thrown in the associated try block.
 
 
subject: exceptions