aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP 6 (kathy Sierra) Chap 5#3 Question Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP 6 (kathy Sierra) Chap 5#3 Question Doubt" Watch "SCJP 6 (kathy Sierra) Chap 5#3 Question Doubt" New topic
Author

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

Vicente de Rivera Iii
Greenhorn

Joined: Apr 23, 2009
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

Joined: Oct 01, 2001
Posts: 2849
    
  11

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

Joined: Apr 23, 2009
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
 
Don't get me started about those stupid light bulbs.
 
subject: SCJP 6 (kathy Sierra) Chap 5#3 Question Doubt