This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt about CH7 Q10 (K&B7)  RSS feed

 
Mohana Avu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!