• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic