aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception question" Watch "Exception question" New topic
Author

Exception question

Karen Liu
Greenhorn

Joined: Sep 14, 2003
Posts: 16
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
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
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.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
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 ]

SCJP 1.4<br />SCWCD 1.4
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130
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


SCJP 1.4, SCWCD<p>Ours is a world where people don't know what they want and are willing to go through hell to get it.<br /> - Don Marquis
Karen Liu
Greenhorn

Joined: Sep 14, 2003
Posts: 16
Thanks you guys!
Happy New Year!
 
Don't get me started about those stupid light bulbs.
 
subject: Exception question
 
Similar Threads
Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)
throw exception question
exam watch question
Exceptions - Handle or Declare requirement
Exception handling throw and throws