• 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

Dan's comprehensive exam 6. Q 16

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ques is
Which statements are true?
a. Assertions should never be placed at any point in the code that should not be reached under normal circumstances.
b. The compiler will generate an error if an assert statement is "unreachable" as defined by the Java Language Specification.
c. A catch clause should not be used to catch an AssertionError.
d. AssertionError is a checked exception.
The ans given are b,c.
I tried to execute the following piece of code.
class b {
public static void main (String[] args) {

try
{
System.out.println("hello world");
throw new AssertionError();
}
catch(AssertionError e)
{
System.out.println("catch");
}
finally
{
System.out.println("finally");
}

}}
The code did not give any compiler error. How should I interpret option c)?
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not catching assertion errors is a contract item rather than requirement.
Which is to say that if you want to go through the trouble of writing asserts into your code, only to catch them before they can alert you to a problem with your code or environment, feel free to do so, just don't be surprised when your colleagues give you confused looks
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you...
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well u r allowed to catch AssertionError i.e compiler will not object but u shud never do this as it defeats the entire purpose of the assertion facility.
u use assertions to test ur logic about something. so if u get an AssertionError u know that ur logic was wrong . I f u wud catch them then u will not get to know about the flaws in ur logic & ur program might break down at a later stage due to wrong logic
for unreachable statements see JLS 14.20
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic