This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes checked exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "checked exception" Watch "checked exception" New topic

checked exception

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131

above is a snippet from the K&B book.
There's a thumb rule saying "any method that throws a checked exception or invokes a () that throws a checked exception, must either declare it or put the () in a try/catch"

dostuff() is declaring the checked exception by saying "void doStuff() throws MyException "

so could I say that the code would have compiled had the calling () been so :-

class MyException extends Exception {
void someMethod () {
}catch (IOException ioe){}

and if this is correct, could someone also write the 'other' alternative , the one with the 'throws' keyword.

Anurag Saksena

Joined: Jul 20, 2004
Posts: 28
2 Ways are..



Not sure if this is what you want to know.

SCJP 1.4
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Thanks, yes that's what I was looking for , one doubt remains:-

// catch way
void someMethod() {try{doStuff();}
catch (MyException me){}}

does it have to be "MyException" in the line "catch (MyException me){}"
would it matter if I had "catch (IOException i){}"[ keeping all the above intact ] ?

similiarly with the declarative way "void someMethod() throws MyException"
could this be a "void someMethod() throws IOException" ?

Basically, I am digging in to know, how far does a user's 'MyException" [user defined checked exception] go ?
[ October 27, 2004: Message edited by: Netty poestel ]
Kali Praveen

Joined: May 27, 2004
Posts: 10
In both the cases you have to use MyException instead of IOException. The reason being you always try to catch Exception that is being thrown. According to your code doStuff() throws MyException. So that should be the one you should be catching. Keep in mind that MyException extends Exception and not IOException( IOException being a subclass of Exception). So you cannot catch IOException in the places where MyException is caught. The compiler complains that IOException is never thrown in scope. You could otherwise say something like


This is valid because MyException is subclass of Exception and not IOException. Hope this clears your doubt.
marc weber

Joined: Aug 31, 2004
Posts: 11343

Also note that if you're catching multiple Exception types, then catch blocks for the most specific types (that is, the "most extended" subclasses) must appear before the parent types. Otherwise, the exception will be caught by the superclass catch block, leaving you with unreachable code (the subclass catch blocks) that will cause a compile-time error.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Kali, Marc, Anurag
Thanks for the lifeline...again.
For the time being this one gets canned !
I agree. Here's the link:
subject: checked exception
It's not a secret anymore!