• 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

Exception output

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic