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().
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 ]
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.
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 methodMoved 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!