Enums With Default Throw Clause?
Posted
by Tom Tresansky
on Stack Overflow
See other posts from Stack Overflow
or by Tom Tresansky
Published on 2010-05-20T19:07:53Z
Indexed on
2010/05/20
19:10 UTC
Read the original article
Hit count: 219
I noticed the following in the Java Language spec in the section on enumerations here: link
switch(this) {
case PLUS: return x + y;
case MINUS: return x - y;
case TIMES: return x * y;
case DIVIDE: return x / y;
}
throw new AssertionError("Unknown op: " + this);
However, looking at the switch statement definition section, I didn't notice this particular syntax (the associated throw statement) anywhere.
Can I use this sort of "default case is throw an exception" syntactic sugar outside of enum definitions? Does it have any special name? Is this considered a good/bad practice for short-cutting this behavior of "anything not in the list throws an exception"?
© Stack Overflow or respective owner