• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

checked exception

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kali, Marc, Anurag
Thanks for the lifeline...again.
For the time being this one gets canned !
reply
    Bookmark Topic Watch Topic
  • New Topic