• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in code with exception

 
Kosh Shah
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


At Point X on line 4, which code is necessary to make the code compile?
A. No code is necessary.
B. throws Exception
C. catch ( Exception e )
D. throws RuntimeException
E. catch ( TestException e)

Answer: B

Could you'll explain me this? Or is there an error in Line 1?

Cheers,
Kosh!
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


runTest throws an Exception which is not a subtype of RunTimeException(those which extend are unchecked exception). Therefore TestException is known as a checked exception.

Since test() runs the runTest method, a TextException a checked exception is thrown inside the method. The jave rule is that checked exceptions should be caught or declared. Caught means the exception occur in a try catch block and is caught by catch. Declared means that the method must declare the exception in the throws clause of the method. In this case the exception is not caught therefore runTest must declare a throws TestException(or a type that is a super type).

unchecked exceptions do not have this restriction

exceptions in the java spec
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me that line 1 and line 2 are out of sequence. And the "Exception Test" should be "ExceptionTest" (without the blank).

Otherwise, TestException is an inner class of ExceptionTest (and not static either).

Where did this question come from?
[ October 03, 2004: Message edited by: Barry Gaunt ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Otherwise, TestException is an inner class of ExceptionTest (and not static either).


Which isn't a problem, is it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic