• 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

Declare or handle exception rule

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code is taken from K&B book. It prints out b. My question - isn't it a requirement for either the Slave class or main method to declare or handle the exception? What if doFileStuff() method throw the exception when called? Will the program die?



Thank you all.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that program will compile as the type of s is not defined in main method. If we ignore that, the main method doesn't need to catch/declare in throws FileNotFoundException. You are calling doFileStuff of Slave class which doesn't declare any exceptions in its throws clause, so main method doesn't need to catch it. doFileStuff in Slave is a valid override of doFileStuff in Master, the overriding method can reduce the number of checked exceptions in the throws clause...
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry... s was declared as a String in the program... typo error on my part.

Thank you for the explanation.
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get it now... if we had s=new Master().doFileStuff(); in the main method instead of s=new Slave().doFileStuff(); then declare or handle exception rule must be enforced... otherwise compile time error.

Thank you Ankit.
 
I think I'll just lie down here for a second. And ponder 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