Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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.
 
Master Rancher
Posts: 3537
39
  • 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: 69399
276
  • 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 ]
 
Maybe he went home and went to bed. And took this tiny ad with him:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic