• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regardless of whether the called method follows "handle and declare" or "handle or declare" rule?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic