File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception output 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 "Exception output" Watch "Exception output" New topic

Exception output

George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34

I was solving some random questions for SCJP online.

I can across this code and had no idea what is the output.

Following are the options:
A. end
B. Compilation fails.
C. exception end
D. exception test end
E. A Throwable is thrown by main.
F. An Exception is thrown by main.

What is the output of the above code?

In the above line of code, under what condition will if be true and when will it be false?

Please give your opinions.



Regards,<br />George Gates
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Badal Choudhary:
... Please give your opinions...

In my opinion...

A boolean literal of true is always true by definition.

This question is about the flow of execution when the AssertionError is thrown. To answer this, you need to know where AssertionError fits into the Throwable hierarchy, and the difference between checked vs. unchecked exceptions/errors. Does that help?
[ October 12, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
I think the best way is to execute the code, having known the meaning of if (true).

Also, have a note on the code catch (Exception ex)... Makes a lot of difference, since you are throwing an Error but catching an Exception.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

E is the correct answer. Since you are manually throwing AssertionError, so there is no need to think about whether assertions are enabled or not. The second thing is that if will always be executed. So when test method is executed, an AssertionError will be thrown. So test will not be displayed.

The AssertionError will not be catched by the main method as AssertionError is a sub-class of Error and not Exception. So end will also not be displayed.

Since AssertionError is not an Exception sub-class, so you will say that a Throwable is thrown as both Errors and Exceptions are sub-classes of Throwable..

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Exception output
jQuery in Action, 3rd edition