• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Handle and Declare rule - Ducking exceptions

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I fix this code to compile it successfully using Handle and Declare rule in

Exceptions


 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This class has two methods.
1. One riskyMethod() that throws and exception
2. One duckingMethod() that calls the riskyMethod.

And now, in the main() method...I am calling the duckingMethod. I know we have to Handle and declare rule - how do I implement this rule in the above code
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


avi sinha
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even a better idea : not just to get the code compiled



avi sinha
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...now I understood the rule Handle & Declare
But in K&B exam watch (page 362) it says

When an object of a subtype of Exception is thrown, it must be handled
or declared.

So...does that mean it can be either handled or declared. How can I apply to my example below.

As said in K&B if I have a userdefined Exception class like this:


Suppose, I am throwing the userdefined Exception in the risky method - How do I handle? How do I declare.
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you have got two options here

1st one : handle the risky method invocation


2nd one eclare that the method (main here) which contains the risky method invocation throws the Exception



for every checked exception either you will have to declare it or handle it (to get compiled successfully)
and all user created exceptions are checked exceptions

avi sinha
 
reply
    Bookmark Topic Watch Topic
  • New Topic