我有一个 Java 7 代码,我正在使用 MethodHanlde .代码是:
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
class HelloWorldApp {
public static void main(String[] args) {
MyMethodHandle obj = new MyMethodHandle();
obj.getToStringMH();
}
}
class MyMethodHandle {
public String getToStringMH() {
MethodHandle mh;
String s ;
MethodType mt = MethodType.methodType(String.class, char.class, char.class);
MethodHandles.Lookup lk = MethodHandles.lookup();
try {
mh = lk.findVirtual(String.class, "replace", mt);
} catch (NoSuchMethodException | IllegalAccessException mhx) {
throw (AssertionError)new AssertionError().initCause(mhx);
}
try {
s = (String) mh.invokeExact("daddy",'d','n');
}
catch(Exception e) {
throw (AssertionError)new AssertionError().initCause(e);
}
System.out.println(s);
return "works";
}
}
当我编译这个时:
javac HelloWorldApp.java
我收到这样的错误:
HelloWorldApp.java:23: error: unreported exception Throwable; must be caught or declared to be thrown
s = (String) mh.invokeExact("daddy",'d','n');
^
1 error
我哪里出错了?
最佳答案
作为 MethodHandle.invokeExact 的 Javadoc州
public final Object invoke(Object... args) throws Throwable
这意味着您必须捕获或“抛出”一个 Throwable
顺便说一句,因为这会抛出一个通用异常,所以可以替代
try {
s = (String) mh.invokeExact("daddy",'d','n');
} catch(Throwable t) {
throw new AssertionError(t);
}
是用
重新抛出Throwable try {
s = (String) mh.invokeExact("daddy",'d','n');
} catch(Throwable t) {
Thread.currentThread().stop(t); // avoids wrapping the true exception
}
如果您停止另一个线程,则使用 Thread.stop(t) 可能无法预测。如果你把它扔给当前线程,它是可以预测的。
注意:您需要确保您的方法为您正在调用的方法“抛出”适当的检查异常,因为编译器无法确保是这种情况。
关于java - MethodHandle - 必须被捕获或声明为抛出。为什么会出现这个错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11707656/