This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Basic doubt in Assertion (from K&B book)

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I had posted the same question long back, but i didn't answer, so posting in detail again.

I read the following line from K&B book:

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.

In the below sample program i can access the AssertionError object. So, can some one please explain what does the above line mean

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare these two errors by executing the following program. Do you notice that there's no stack trace for the assertion ? The only thing you can get is the message included in the assertion, using getMessage().
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Chris for this wonderful example.
 
Ranch Foreman
Posts: 3297
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe, your example doesn't seem to work for me. If I run the code without assertions enabled (which is the default if I don't specifically enable them), then generateAssertionError() does nothing, and not even getMessage() is useful because there's never any AssertionError object created. However if I run with assertions enabled (e.g. using java -ea), then I get exactly the results I would expect to get, which are pretty much the same as if I'd created and thrown a new AssertionError directly. Including the stack trace. Did you just forget to enable assertions, or are you really seeing something very different than I am?
[ October 24, 2008: Message edited by: Mike Simmons ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, you really think I'm dumb or what ? Well, you're right, I am I didn't turn assertions on.

Ok, I'm trying to cover my error What the book means is that unlike other exception, AssertionError does not allow you to pass the cause of the error. If you look at the available constructors, there are only constructors allowing you to pass a message.

I quit the programming world. I'm on my way to a monastery. Amen.
 
Marshal
Posts: 65019
246
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real reason assertions don't work like ordinary Exceptions is that you aren't supposed to use assertions in real life. They are for testing. You put lots of assertions in your code, run it, and wherever you get an AssertionError you have found a vulnerability. You then put an ordinary throw statement and an ordinary Exception in the code there.If you run that without assertions you will never know somebody has passed a ridiculous argument to the heat method, and violated the class invariant. If you run it with -ea you will get the error. You then add the following if statement to the beginning of the heat method[edit]Moved assertion around to correct syntax error[/edit]
[ October 24, 2008: Message edited by: Campbell Ritchie ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!