Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 6 Question 20, pg 332 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Tami Wakana
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The question asks what does the output of the code contain and the answer is "abce" and "An exception with the message set to "3"". I understand the exception with the message set to "3", but I don't understand why the output is just abce. Shouldn't it include "d" since line 8 throws a RuntimeException and there is a catch block that takes in a RuntimeException?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tami Wakana wrote:The question asks what does the output of the code contain and the answer is "abce" and "An exception with the message set to "3"". I understand the exception with the message set to "3", but I don't understand why the output is just abce. Shouldn't it include "d" since line 8 throws a RuntimeException and there is a catch block that takes in a RuntimeException?

No, the output of that code snippet is actually "abce" and an uncaught RuntimeException with a message set to "3".

And the reason for this is very simple: the catch blocks/handlers only catch possible exceptions thrown from within the try block. The RuntimeException is not thrown from the try block but from within a catch block/handler. And that's why this exception is not caught by the catch block/handler of RuntimeException (on line9).

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic