• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Exception question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks:
I have a question about Exceptions.
Here is a piece of code from K&B book p252
class MyException extends Exception{
void someMethod() {
doStuff();
}
void doStuff() throws MyException{
try{
throw new MyException();
}
catch(MyException me){
//throw me;
}
}
}
This code won't compile.
The book says "Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception."
doStuff already handles the exception by providing a try/catch block, why someMethod still needs to throw this exception to make the code compile? If doStuff handls this exception, the exception won't be passed up to the calling method (someMethod), will it?
another Q: modified the code by commenting throws clause out
void doStuff() //throws MyException
{
try{
throw new MyException();
}
catch(MyException me){
//throw me;
}
}
it works well. My question here is:
If throws clause was there, compile error; if No throws clause, no error.
why is that? throws clause is a declration, what kind of affection it has to the code?
Thanks
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karen,
When you have a throws clause that comes with a checked exception in a method signature, you're instructing the compiler that this method might throw a checked exception. The implication is that every call to this method must either be made inside a guarded region or the caller method must declare the exception in its signature to pass it up the stack. Whether you actually throw an exception inside the method that throws it, doesn't matter. An illustrating example of why you still need to declare-or-handle the exception from your code is polymorphism. Consider this code:

doTest() is overridden in Child so as not to throw an Exception. However, polyTest method doesn't "know" which type of object will come in polymorphically. As a result, the guarded region in polyTest is necessary.
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karen.

The handle-or-declare rule states that you must either handle a checked exception or declare that it to be thrown.
Using your code above, an example of a handle part of the handle-or-declare rule:
At Line 9, a checked exception is thrown in the gaurded region (meaning inside the try block).
At Line 11, the exception thrown is caught and handled.
Although it is allowed, the "throws MyException" at line 7 is no longer a strict requirement since the exception is already handled within doStuff method. The code above is an example of re-throwing an exception. This means the doStuff method is designed to handle the exception, process it, and then re-throws that same exception to the doStuff caller. Hence, the throws clause at line 7. But why re-throw the exception when it already handled? If for some reasons someMethod (the doStuff caller) would also like to know if an exception occurred or not. It is possible someMethod may have important logic of its own to execute in the event of an exception within doStuff.
In this case since Line 7 declares (declare part of handle-or-declare rule) it throws an exception of type MyException (which is a checked exception), the caller (someMethod) is required to handle the exception thrown by method doStuff or declare it to be thrown.
In order for the code to compile without changing the original doStuff method, someMethod must do either one of the following (handle-or-declare rule):
- Declare it throws an exception

- Handle the exception

Or again if for some good reasons, do both (re-throw the exception that was caught).

Hope this helps.
[ December 22, 2003: Message edited by: dennis zined ]
[ December 22, 2003: Message edited by: dennis zined ]
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since doStuff() method cannot handle the exception, it throws back the exception from its catch block to the calling method i.e someMethod() , So now the the someMethod must handle the exception, so it has to declare that it throws even if it has try -catch block., Just assume there could be another method calling someMethod()....needs to know that.
Hope that helps...
Thanks
Venkatesh
 
Karen Liu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you guys!
Happy New Year!
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic