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?

有帮助吗?

解决方案

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; 
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top