• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic doubt in Assertion  RSS feed

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read the following lines in one book, regarding Assertion:
"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.
******************************* "

I am not able to understand the lines embedded in stars(*). Some one please explain this...???
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,
Welcome to JavaRanch!

As stated in the first section, it is bad practice to catch Errors (rather than Exceptions.) However it is possible and the authors of Java recognize that people will do something just because they can even if it is recommended against. So they tried to make catching an AssertionError to be as useless as possible. Not giving you access to the object that caused the AssertionError to be thrown makes it harder to use. Often you want to be able to do something with a caught Exception.
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanka a lot for the reply...
Sorry for not putting my doubt very clearly. The statement says, we do not have access to AssertionError object. See program below:

public class test
{
public static void main(String args[])
{
try{
int i = 10;
assert (i > 10);
}catch(AssertionError ae){
ae.printStackTrace();
}
}
}
Here i'm able to use the Assertion Error object (i.e. i have access to the object). This is all we can do even with Exception object. So, what exactly those lines mean...???
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!