Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic doubt in Assertion (from K&B book)

 
Aravind Jerubandi
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

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • 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 Ubuntu VI Editor
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic