aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)" Watch "Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)" New topic
Author

Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

Why does the following code throw a compiler error on line someMethod() (called from callSomeMethod())?

The someMethod() method declares the MyException and also handles it...so why would we need to even handle the same exception in callSomeMethod()?

Here's code



Thanks
Chintan


SCJA 1.0
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Because someMethod() declares that it throws an exception. The fact that is doesn't is irrelevant. You can fix it by catching it in a try-catch block or declare to throw the same or a wider exception.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
So, can we say that even if a method is declaring and handling exception type, the calling method has to either re-declare or catch it?

Thanks
Chintan.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

No. When a method is declaring to throw a checked exception, the calling method must catch it or declare to throw the same or a wider exception
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Regardless of whether the called method follows "handle and declare" or "handle or declare" rule?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Yes. You only have to look to the method signature. It doesn't matter what happens inside of the method (see it as a black box). If the signature declarers to throw a checked exception then the calling method must catch it, or declare to throw the same or a wider exception.
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
yes, it IS following "handle or declare" rule. "callSomeMethod()" method in "MyException" class is calling a method "someMethod()" which declares that it throws an exception, so now "callSomeMethod()" must either handle it or declare it!


SCJP 5.0 100%
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Thanks a lot guys...my doubt is now clarified.

Wouter, lightning struck my mind when I read "black box" scenario. So, thnx much dude.

The reason for posting out this question was due to my understanding that if you throw a new exception from a catch block and it is not declared in throws clause, the compiler complains...that's why I thought the compiler would be intelligent enough to see that if a called method(in this case someMethod()) abides by "Handle and Declare" rule then none of methods calling it should ever handle those exception as the exception will be caught and taken care of in called method(someMethod()) itself...does my argument makes any sense?

My doubt has been clarified, however, I still want to know opinion of experts out there on my argument.

Basically, if compiler is intelligent enough to throw compile time error on an exception(not declared in throws clause) thrown in catch clause .....why is it not intelligent enough to see that if a method is abiding by "handle and declare" rule for the exception, then all calling methods need not to catch that exception(unless its re-thrown)..and thereby not give any compile time error message.

Thanks
Chintan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on "Handle Or Declare" rule for Exceptions - K&B book Page 374 (Exam Watch)