• 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

Exception Doubt pls help

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the following code


In this case doThat() method is handling the exception and not declaring it... and also doThis() is not declaring it... it works...
why is the handle or declare rule not working with doThis() method it is still not declaring..


But if we change this code to



doThat() is both declaring as well as handling the exception ..now i get a compiler error stating the call to doThat() in doThis() is not handled.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The handle-or-declare rule (in context of a method M) applys to the checked exceptions that are thrown in the method M , or that are declared by the methods called inside M.

In the first case, the doThat() method throws and handles the exception. Since the checked exception that might be thrown is handled, the handle-or-declare rule is seen in action here. Now, for doThis() method, no checked exception gets thrown in the method. Also, none of the methods that doThis() calls declares any of the checked exception. Hence doThis() method neither needs to declare any exception, nor needs to catch any exception. (Note that the exception that might be thrown in doThat() method is never escaped from doThat() method and hence doThis() method is not even aware of this exception. )

In the second case, you are explicitly declaring that exception of class Exception (or its any subclass) might escape doThat() method. In this case, doThis() method needs to either handle or declare this exception according to the rule stated above.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first code, the method throws and catches the exception so there is nothing to propogate to doThis().

In the second code, since you declare that doThat throws Exception, and Exception is a checked exception, doThis must either catch and handle an Exception or declare that it throws Exception.
 
reply
    Bookmark Topic Watch Topic
  • New Topic