Java Reference
In-Depth Information
Using a tHRow is especially valuable for methods that return values.
Without a throw the compiler would not know that the method cannot
get to the bottom, so it insists that you return a value at the end. That
code typically looks like this:
return -1; // never happens
The comment "never happens" asserts something to be true but doesn't
check or enforce it. Only a human reading the code knows the invariant.
And an assertfalse could be turned off, so the compiler would still re-
quire the code to have a bogus return . The throw enforces what you
already know.
The compiler will not let you have an assert statement it believes to be
unreachable, just like it won't let you have any other unreachable state-
ment. When the compiler can determine that a line is unreachable, not
only do you need no assert , you may not put one there.
 
Search WWH ::




Custom Search