문제

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