There is no way in the Java method signature so that you can tell the compiler that the method cannot return. Without this, the compiler is unable, except to assume that it can return. (Indeed, the JLS reachability / specific purpose rules will state this explicitly ... if you want to get through them.)
I hesitate between two approaches:
int fun() { ... error(); return 0;
and
int fun() { ... error(); throw new AssertionError("not reached"); }
None of them are completely satisfactory.
In the first version, the comment is very important, because someone who first read your code may not understand that error never returns.
The second version is more reliable, as it will give you a βquick crashβ if someone changes the behavior of the error method so that it really returns. (The first version will cause the fun method to return a dummy value ... possibly leading to other unforeseen consequences.)
At a connected point, a method that pulls out a JVM fork by calling System.exit() can be problematic. The best idea is to throw the end of the world exception and catch it at the extreme level of your main thread. For exceptions superimposed on other threads, one idea would be to install a default exception exception handler that uses Thread.interrupt() to notify the main thread that an βend of the worldβ exception has been thrown.
But the fact is that System.exit() calls made deep in the code are problematic; for example, if you reprogram your code to work in a larger structure; for example in a web container.
source share