Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions - Handle or Declare requirement

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • 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 ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic