• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Assertions in java 1.5

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I was going through the book SCJP Certification [] chapter 5. In page 380 I found the below statement .
================================================================>
As with so much of Java, you can abuse the intended use of assertions, despite the best efforts of Sun's Java engineers to discourage you from doing so. For example, you're never supposed to handle an assertion failure. That means you shouldn't catch it with a catch clause and attempt to recover. Legally, however, AssertionError is a subclass of Throwable, so it can be caught. But just don't do it! If you're going to try to recover from something, it should be an exception.

To discourage you from trying to substitute an assertion for an exception, the AssertionError doesn't provide access to the object that generated it.

All you get is the String message.
================================================================>
But I have tried the below statement and it seems to compiling/running properly. Am I missing something? Appriciate your help.


================================================================>
public class S
{
public static void main(String args[])
{
try
{
int i=128;
assert (i==127);
i++;
}
catch(AssertionError e)
{
e.printStackTrace();
}
}
}

output:
> "C:\Program Files\Java\jdk1.5.0_06\bin\java.exe" -ea S
java.lang.AssertionError
at S.main(S.java:8)
================================================================>


Appriciate your help.

Thanks and Regards,
sridhanya
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not supposed to catch errors. Errors are things that are so bad that, you should not catch them and try to recover. However you can catch errors and handle them, which is bad programming practise. It is legal but not a very smart thing to do.
 
sridhanya ganapathy
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do agree with it.
Error can be caught.
But in the book the phrase was like the below

-----------------------------------------------------------
To discourage you from trying to substitute an assertion for an exception, the AssertionError doesn't provide access to the object that generated it.
-----------------------------------------------------------

But i am able catch the same object and printStackTrace gives me the appropriate message.Please help me in this regard
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why doesn't an AssertionError allow access to the object that generated it?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic