This week's book giveaway is in the Functional programming forum.
We're giving away four copies of A Functional Approach to Java: Augmenting Object-Oriented Java Code with Functional Principles and have Ben Weidig on-line!
See this thread for details.
  • 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic