• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in code with exception

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic