Question

The throws list of a method obviously plays a role during compilation. Does it however have any impact whatsoever during runtime?

For instance, if I have a method without a throws list and use dark bytecode magic to summon and throw an IncrediblyCheckedException, would it throw it without question or would the runtime validator notice this and throw an error or something instead?

Was it helpful?

Solution

It has no effect on the runtime whatsoever. Case in point:

Thread.currentThread.stop(new Exception());

can be written anywhere and the line of code will throw the exception.

The above will throw the exception from native code, but here's another trick with a plain Java throw:

public static void main(String[] args) {
    CurrentClass.<RuntimeException>sneakyThrow(new Exception());
}
@SuppressWarnings("unchecked")
private static <E extends Throwable> void sneakyThrow(Throwable t) throws E { 
  throw (E)t; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top