java - 为什么 NativeMethodAccessorImpl 声明 InitationTargetException

标签 java exception invocationtargetexception native-methods

NativeMethodAccessorImpl invoke method声明它可以抛出 IllegalArgumentExceptionInitationTargetException

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 方法 throws IllegalArgumentException在某些情况下,例如

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

但我没有看到任何抛出 InvocableTargetException 检查异常

的选项

native 方法 invoke0(不声明异常)是否可以抛出 InvocableTargetException

或者是由于方法签名向后/ future 兼容性而保留 InitationTargetException 吗?

最佳答案

invoke0可以抛出InitationTargetException:

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 资源文件时。

关于java - 为什么 NativeMethodAccessorImpl 声明 InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257618/

相关文章:

java - ClassNotFoundException 但类位于类路径上且位于同一个 jar 中

java - 是否有任何注释生成每个步骤从自动化测试执行所需的时间

c# - 仅在 Visual Studio 2015 中的 Debug模式下抛出 WebDriverWait 异常

java - LocalDateTime 解析抛出 "java.lang.IllegalArgumentException: Unknown pattern letter: T"

java - Spring /hibernate : InvocationTargetException when deleting entity

java - 通过java将数据追加到xlsx文件中

java - Java 中的 boolean 变量存储什么? (根据内存、位模式)

android ksoap调用xmlpullparser异常

spring - bean 名称 'login' 的 BindingResult 和普通目标对象都不能用作请求属性

java - 使用 JavaFX set 和 get 方法时出现 InloggingTargetException