wood burning stoves 2.0*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "checked exception" Watch "checked exception" New topic
Author

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 () {
try{
doStuff();
}catch (IOException ioe){}
}

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

TIA
Anurag Saksena
Greenhorn

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

1)


2)


Not sure if this is what you want to know.

-a.
=========
SCJP 1.4
SCWCD 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 ?
TIA
[ October 27, 2004: Message edited by: Netty poestel ]
Kali Praveen
Greenhorn

Joined: May 27, 2004
Posts: 10
Netty,
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


or


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

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
sscce.org
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: http://aspose.com/file-tools
 
subject: checked exception
 
Similar Threads
throwing Exceptions
Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)
Exceptions - Handle or Declare requirement
throw exception question
Exceptions doubt