• 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 catching

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


Answer : 1
Explanation :
Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() bechanged to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur.

My question:
In the main method, throwMethod() is called; there is a catch block for catching this.
Then, why do we need the thros clause, as specified in the explanation?
Can't the exception thrown by the call to throwMethod() be handled by the catch block in the main() method itself?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During compilation time, the compiler looks for a mechanism to handle the thrown exception either by means of
1. try catch block or
2. throws in the method declaration for the calling program to handle it.
Both of these are missing in your program.
Otherwise how will the exception be handled? If you want the calling program to handle the exception then use the throws in the method declaration or use try catch if it can be handled in the current program block.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic