Exceptions - Handle or Declare requirement
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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 ]
Thanks, leo
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 22 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Thanks, leo
| New rule: no elephants at the chess tournament. Tiny ads are still okay. The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









