Win a copy of Design for the Mind this week in the Design forum!

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

Vicente de Rivera Iii
Greenhorn
Posts: 9
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

Greg Charles
Sheriff
Posts: 2985
12
• 1
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
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