Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in code with exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in code with exception" Watch "Doubt in code with exception" New topic

Doubt in code with exception

Kosh Shah

Joined: Jul 20, 2004
Posts: 19

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?

Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175

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

MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Barry Gaunt:
Otherwise, TestException is an inner class of ExceptionTest (and not static either).

Which isn't a problem, is it?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Doubt in code with exception
It's not a secret anymore!