Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

SCJP 6 (kathy Sierra) Chap 5#3 Question Doubt

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From SCJP 6 study guide (kathy Sierra) cert book,

Chap 5#3

Given:

try { int x = Integer.parseInt("two"); }

Which could be used to create an appropriate catch block? (Choose all that apply.)
A. ClassCastException
B. IllegalStateException
C. NumberFormatException
D. IllegalArgumentException

The book's answer is C & D w/c I see is a legal catch.

My answer was C, I thought catching the most specific is appropriate. Am I wrong?

Thanks in advance! First time post here, I'll be taking the exam SCJP6 Next week
 
Sheriff
Posts: 3036
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, NumberFormatException is the exception that would be thrown, but IllegalArgumentException is its superclass, so catching that would also do the job. The word "appropriate" is somewhat subjective, but I'd argue both are appropriate here, though I might waffle a bit if catching Throwable would also be. From what I remember about SCJP though, it's not trying to trick you with fine points about programing style.

Note: it's Kathy Sierra. Kathy Bates is the one who bashed in James Caan's knees in Misery
 
Vicente de Rivera Iii
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
corrected Bates to Sierra

Greg Charles wrote:Yes, NumberFormatException is the exception that would be thrown, but IllegalArgumentException is its superclass, so catching that would also do the job. The word "appropriate" is somewhat subjective, but I'd argue both are appropriate here, though I might waffle a bit if catching Throwable would also be. From what I remember about SCJP though, it's not trying to trick you with fine points about programing style.

Note: it's Kathy Sierra. Kathy Bates is the one who bashed in James Caan's knees in Misery

 
I've never won anything before. Not even a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic