File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Basic doubt in Assertion (from K&B book) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic doubt in Assertion (from K&B book)" Watch "Basic doubt in Assertion (from K&B book)" New topic
Author

Basic doubt in Assertion (from K&B book)

Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
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



-Aravind<br />SCJP 5.0 (91%), Preparing for SCBCD.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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().


[My Blog]
All roads lead to JavaRanch
Aravind Jerubandi
Ranch Hand

Joined: Dec 17, 2006
Posts: 49
Thanks a lot Chris for this wonderful example.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic doubt in Assertion (from K&B book)
 
Similar Threads
Assertion Doubt
AssertionError
Basic doubt in Assertion
Question on Assertion from KnB's CD
Using Assertions Appropriately - page 392 of SCJP6 K&B book