MethodHandle - must be caught or declared to be thrown. Why this error?

I have a Java 7 code, where I'm playing with MethodHanlde. The code is :

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";
    }
}

when I compile this :

javac HelloWorldApp.java

I get a error like this :

HelloWorldApp.java:23: error: unreported exception Throwable; must be caught or declared to be thrown
            s = (String) mh.invokeExact("daddy",'d','n');
                                       ^
1 error

Where I'm making mistake?

Answer:1

As the Javadoc for MethodHandle.invokeExact states

public final Object invoke(Object... args) throws Throwable

This mean you mush catch or "throws" a Throwable

BTW As this throws a generic exception an alternative to

    try {
        s = (String) mh.invokeExact("daddy",'d','n');
    } catch(Throwable t) {
        throw new AssertionError(t);
    }

is to rethrow the Throwable with

    try {
        s = (String) mh.invokeExact("daddy",'d','n');
    } catch(Throwable t) {
        Thread.currentThread().stop(t); // avoids wrapping the true exception
    }

While using Thread.stop(t) can be unpredictable if you stop another thread. it is predictable if you throw it for the current thread.

Note: You need to ensure your method "throws" the appropriate checked exception for the method you are calling as the compiler cannot ensure this is the case.

Answer:2

OSX apps have a Help menu on the right of the menu bar. When the Help menu is click, a menu opens with a search box at the top and a few menu items under it. How can this be achieved in a Java Swing ...

I need a rounding function that returns a larger integer when the input is positive and the a smaller number when negative, i.e. it should not return 0 unless the input is actually 0.0. Examples: f(...

I am working on a problem which requires me to fetch a list of all Kafka topics present in a cluster and also get their corresponding partition number. I know that this can be done through kafka-...

I have a Java Applet compiled in Eclipse using Swing. See here: http://www.test.world2build.com/Game/Play.aspx For the start of the applet, I made a "Loading..." dialog that would login and download ...