• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

got struck with a question. please help me friends.

 
K Vidhyakar
Ranch Hand
Posts: 68
  • 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.

here optian A and D seems to be the correct ans. but i cant figure out the one which is right.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vidhyakar vidhyakar,"

Please revise your display name to meet the JavaRanch Naming Policy. You've been asked twice before in previous posts -- and we take this policy seriously. You can edit your name here.

Thank you for your prompt attention!

Now, as to the question...

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

Consider that an unchecked RuntimeException is an Exception, so D is false. Furthermore, even a statement that throws a checked Exception does not need to be in a try block if its enclosing method declares the Exception.
 
Sandeep Singh Kunwar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Any statement that can throw an Exception must be enclosed in a try block.

This can not be right as:


without using try-catch it compiles fine!! as its throwing Exception while method defination block
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic