Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regardless of whether the called method follows "handle and declare" or "handle or declare" rule?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic