• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exception Question

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A,D
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic