Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception output

 
George Gates
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic