• 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

Exceptions - Handle or Declare requirement

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I don't catch an exception in a method that throws a checked exception, it is supposed to propagate back to the calling method, right?
But if I do catch an exception in a method that throws a checked exception, then why do I have to declare that the calling method throws an exception in the called method?
Example from Kathy and Bert:

If I am catching the MyException in doStuff, it isn't propagating back to the calling method of someMethod, so why do I have to declare that someMethod throws MyException?
Is it because I'm rethrowing the exception in the catch clause?
[ June 16, 2003: Message edited by: leo donahue ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi leo
the problem is your catch() stmt,
catch(MyException me){
throw me;
}
that says that when the MyException will be catched, it will be thrown again...now when would it go from catch?? to the calling method (or outer block) right? and the calling method is someMethod() so u have to catch it there or throw it from that method.
here u r not "handling" the exception in a way that gets rid of it. the exception "me" is "re-thrown" from the catch. so basically its equal to write "throws MyException" in the doStuff() method instead of trying to catch and then throw the exception again...
do u get me?
regards
maulin
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maulin Vasavada:
hi leo
the problem is your catch() stmt,
catch(MyException me){
throw me;
}
that says that when the MyException will be catched, it will be thrown again...now when would it go from catch?? to the calling method (or outer block) right? and the calling method is someMethod() so u have to catch it there or throw it from that method.
here u r not "handling" the exception in a way that gets rid of it. the exception "me" is "re-thrown" from the catch. so basically its equal to write "throws MyException" in the doStuff() method instead of trying to catch and then throw the exception again...
do u get me?
regards
maulin


Hi maulin,
I follow you.
I put a finally clause after the catch stmt to see if finally *always* runs, it doesn't. I guess this is one of "those times" when finally doesn't run.
Thanks for your help
reply
    Bookmark Topic Watch Topic
  • New Topic