• 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

Doubt about CH7 Q10 (K&B7)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 7 (Assertions and Java7 Exceptions) Q&A Self test Question : 10

The answer is marked as D.
Shouldn't this be F : RuntimeException c with suppressed RuntimeException a
If I change the program to display suppressed exceptions as follows -




I get the output -
Supp : java.lang.RuntimeException :a
Exception in thread "main" java.lang.RuntimeException: c
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No! The answer and explanation in the book or spot-on.

When you run the program unchanged, you'll get this output:
Exception in thread "main" java.lang.RuntimeException: c
at Animals.run(Animals.java:38)
at Animals.main(Animals.java:32)


And that exactly matches with option D: RuntimeException c with no suppressed exception.

If you would make some small modifications to the code snippet, so you simply could rethrow the exception from the catch block as shown in this code snippet:


Then you get this output:
Exception in thread "main" java.io.IOException
at AnimalsForum.run(Animals.java:14)
at AnimalsForum.main(Animals.java:10)
Suppressed: java.lang.RuntimeException: a
at AnimalsForum$Lamb.close(Animals.java:6)
at AnimalsForum.run(Animals.java:15)
... 1 more


And this output matches exactly with option A (not F): IOException with suppressed RuntimeException a

The explanation in the book explains this perfectly: the exception caught by the catch block matches A (an IOException with a suppressed RuntimeException a), but this exception is completely ignored. The catch block just throws a (new) RuntimeException c (with no suppressed exceptions).

Hope it makes some sense and clears your doubt!
Kind regards,
Roel
 
reply
    Bookmark Topic Watch Topic
  • New Topic