Re: Java language and library suggestions
On Sun, 19 Jul 2009, Patricia Shanahan wrote:
Tomas Mikula wrote:
...
try {
encoder.writeEncoded(obj);
} catch(IOException e) {
throw new AssertionError(e);
}
...
I would not use AssertionError for this, for several reasons:
1. It has a defined meaning, "Thrown to indicate that an assertion has
failed.", that this usage does not match.
2. It breaks a key invariant for AssertionError. It is not thrown from
code that is run with assertions disabled.
I don't think that meaning or that invariant are as set in stone as you
think they are. Sun suggest exactly the usage Tomas is using - find for
"throw new AssertionError":
http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html
This is very much an example of using the assertion mechanism to check
flow control - the AssertionError is only fired if an allegedly impossible
branch happens.
3. Passing e to the AssertionError constructor forces the message to be
e.toString(). Is that the most useful message for explaining what is
going on?
That's a good point. Sadly, AssertionError doesn't have a (String,
Throwable) constructor, but you could do:
catch (IOException e) {
AssertionError ae = new AssertionError("an exception which was declared to be impossible occurred"); // or better
ae.initCause(e);
throw ae;
}
tom
--
Hit to death in the future head