Sma*_*ker 2 java bytecode exception
该throws方法的名单显然起着编译过程中的作用.然而,它在运行时是否有任何影响?
例如,如果我有一个没有throws列表的方法并使用黑暗的字节码魔法来召唤并抛出一个IncrediblyCheckedException,它会毫无疑问地抛出它,或者运行时验证器会注意到这个并抛出错误或者其他东西吗?
它对运行时没有任何影响.例证:
Thread.currentThread.stop(new Exception());
Run Code Online (Sandbox Code Playgroud)
可以写在任何地方,代码行将抛出异常.
上面将从本机代码中抛出异常,但这是使用普通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;
}
Run Code Online (Sandbox Code Playgroud)