• 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

Basic doubt in Assertion

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...???
 
author & internet detective
Posts: 41506
881
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Aarav Thomas
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...???
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic