wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception output" Watch "Exception output" New topic
Author

Exception output

George Gates
Ranch Hand

Joined: Jul 11, 2008
Posts: 34
Hi,

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.

Thanks

Regards,
Badal


Regards,<br />George Gates
marc weber
Sheriff

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
sscce.org
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Badal,
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception output
 
Similar Threads
Q on Exception Handling
Question from mock site
Doubt in Error/AssertionError
explain AssertionError
About Exception and Throwable