Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Question

 
Sim Kim
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statement is true?

A. catch(X x) can catch subclasses of X.
B. The Error class is a RuntimeException.
C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.
E. Any statement that can throw a RuntimeException must be enclosed in a try
block.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A,D
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see this type of question a lot and it really is ambiguous.

In answer B, "a RuntimeException" means "a subclass of RuntimeException".

In answer D, if "an Exception" means "only an instance of class Exception", D is true. If "an Exception" means "an instance of the class Exception or any subclass of Excepton", the D is not true.

Luckily, on the real exam we are told the number of answers expected.

If we take literally, "Which statement is true?", only one answer is expected and it is A.

[ January 12, 2005: Message edited by: Mike Gershman ]
[ January 12, 2005: Message edited by: Mike Gershman ]
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"an Exception" means "an instance of the class Exception or any subclass of Excepton", the D is not true.


I dont understand why is it not true. Is it because RuntimeException is subclass of Exception, so it is possible that subclasses of RTException may be thrown.
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I see it, none of the last three (C-E) can be true because the statement that throw something can be inserted in a method that throws something.

 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep got my point but I think J Isberg really nailed the answer. The key word is "must". A throws clause is an alternative to a try block.

I hope this kind of question is not on the SCJP exam. It's more like a riddle than a test of knowledge. Somone who is well aware of throws clauses (like myself and the other above posters) were fooled by the wording into answering a different question than the one asked.

Some misleading items, like incorrect indentation and deceptive naming of class references, are realistic reflections of life as a programmer. Tricky wording of questions is just an easy way to get some spread in the scores.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lavjeet Khanuja:
Which statement is true?


C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.
E. Any statement that can throw a RuntimeException must be enclosed in a try
block.


I think all 3 are wrong ... because we can avoid try/catch by using throws ...
[ January 12, 2005: Message edited by: rathi ji ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic