Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throws vs Java Bytecode

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?

like image 566
BambooleanLogic Avatar asked Jul 21 '26 04:07

BambooleanLogic


1 Answers

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; 
}
like image 193
Marko Topolnik Avatar answered Jul 23 '26 19:07

Marko Topolnik