InvocationTargetException

InvocationTargetException

NativeMethodAccessorImpl invoke method声明它可以抛出IllegalArgumentExceptionInvocationTargetException


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);



在某些情况下,例如本机方法throws IllegalArgumentException


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



但我看不到引发InvocationTargetException检查异常的任何选项

可以通过本机方法InvocationTargetException(未声明异常)抛出invoke0吗?

还是由于方法签名向后/将来兼容而保留InvocationTargetException吗?

最佳答案

invoke0可以抛出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)


例如,当找不到FXML资源文件时。

07-27 17:28