2

我有一个 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

我在哪里犯错?

4

2 回答 2

5

正如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) 可能无法预测。如果你把它扔给当前线程是可以预测的。

注意:您需要确保您的方法为您正在调用的方法“抛出”适当的检查异常,因为编译器无法确保是这种情况。

于 2012-07-29T08:02:11.990 回答
1

invokeExact被宣布throws Throwable。因此,您需要 catchThrowable而不是Exception. (AnException只是 的一种Throwable。)

try {
    s = (String) mh.invokeExact("daddy",'d','n');
}
catch(Throwable t) {
    throw new AssertionError(t);
}
于 2012-07-29T07:59:49.180 回答