The biggest difference between
assert false;
(Brackets are not needed; assert not a function, but an expression.) And
throw new RuntimeException();
lies in the fact that this statement can be turned off. In fact, it is disabled by default if the JVM is not running with the -ea flag ("enable assertions"). If assertions are included, assert false unconditionally throw an AssertionError that comes from Error . But since claims can be disabled, there are two problems:
- the error may go unnoticed and
- Parsing a control stream requires a
return dummy return after assert (which is mostly messy).
Therefore, in the above case, I would certainly go with explicit (and shorter)
throw new AssertionError("invalid type " + type);
instead of assert followed by a return dummy.
As mentioned in the comments, it is assumed that type is an internal parameter, and an invalid value indicates an error in the logic itself. If it is an input parameter, it should be checked in accordance with normal rules and an IllegalArgumentException should be thrown if the check is not completed.
source share