3 ms·
Also in Java: Switching or if-else chains on an `enum`. It's still a good practice to include a final `else` or `default` case, but it should really never happe
by jdmichal 11y ago
Also in Java: Switching or if-else chains on an `enum`. It's still a good practice to include a final `else` or `default` case, but it should really never happen. Actually, inclusion of the `default` case will be enforced by the compiler if it can detect a code path that doesn't return. [0]
enum Whatever { FOO, BAR }
if (whatever == FOO) {
} else if (whatever == BAR) {
} else {
// Should never happen!
}
[0] http://stackoverflow.com/questions/5013194/why-is-default-required-for-a-switch-on-an-enum-in-this-code http://stackoverflow.com/questions/5013194/why-is-default-re...
- jrgv 11y agoI'd prefer to throw an exception in that else block: throw new AssertionError("Should never happen");
- jdmichal 11y agoI just put a comment as an example. I typically log-and-throw.
- lancefisher 11y agoThis can happen when some other developer adds to your enum not knowing about the use.
- deleted 11y ago[deleted]