javaexceptioninvocationtargetexceptionnative-methods

Why NativeMethodAccessorImpl declare InvocationTargetException


NativeMethodAccessorImpl invoke method declare it can throws IllegalArgumentException or InvocationTargetException

public Object invoke(Object obj, Object[] args)
    throws IllegalArgumentException, InvocationTargetException
{
    // We can't inflate methods belonging to vm-anonymous classes because
    // that kind of class can't be referred to by name, hence can't be
    // found from the generated bytecode.
    if (++numInvocations > ReflectionFactory.inflationThreshold()
            && !ReflectUtil.isVMAnonymousClass(method.getDeclaringClass())) {
        MethodAccessorImpl acc = (MethodAccessorImpl)
            new MethodAccessorGenerator().
                generateMethod(method.getDeclaringClass(),
                               method.getName(),
                               method.getParameterTypes(),
                               method.getReturnType(),
                               method.getExceptionTypes(),
                               method.getModifiers());
        parent.setDelegate(acc);
    }
    return invoke0(method, obj, args);
}
private static native Object invoke0(Method m, Object obj, Object[] args);

Native method throws IllegalArgumentException in some cases, e.g.

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

But I don't see any option for throwing InvocationTargetException checked exception

Can InvocationTargetException be thrown by native method invoke0 (which doesn't declare exceptions)?

Or is InvocationTargetException remains because of method signature backward/future compatibility ?


Solution

  • invoke0 can throw InvocationTargetException:

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    

    For example when FXML resource file was not found.