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