• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why this exception handling works?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an exam question from CertPal:


It is understandable that line #3 will give a complier error. So I changed Exception to MyException in both line #3 and #4 and the program compiled. Then just as a quick try, I changed MyException again to RuntimeException in both lines #3 and #4 and the program also compiled. This was a bit surprsie to me as RuntimeException is a superclass of MyException and the eat() method in interface Bread throws MyException, which is lowest one in the hierarchy. To me, it seems only MyException should work here. Help please!
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

RuntimeException is an unchecked exception. The rule 'An exception higher up the class hierarchy is not allowed' only applies for checked exceptions.

John
 
Bing May
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response, John. It's really helpful.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Day,

Supporting John's statement my beloved outstanding book - Java 2 by Bill Brogden says on page #133 –

....you can see that I derived AssertionException from the class RuntimeException. This means that the compiler will not check for the appropriate exception-handling mechanism in the surrounding code. You can put in the try-catch structure to catch an AssertionException, but the compiler will not insist on it.



Regards,
Dan
 
What do you have to say for yourself? Hmmm? Anything? And you call yourself a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic