java · 2025-05-31 0

InvocationTargetException 的产生

InvocationTargetException 通常在使用 Java 反射机制调用方法时出现,当被调用方法本身抛出异常时,Java反射机制会将原始异常封装为 InvocationTargetException 异常抛出。

动态代理会使用反射,所以动态代理也会遇到 InvocationTargetException 异常。

InvocationTargetException 异常提供了一个重要的方法 getCause(),该方法返回原始异常,也就是被调用方法抛出的异常。

为什么不直接抛出真实异常?因为 invoke 方法不能改变签名,只能抛 InvocationTargetException 包装它。

InvocationTargetException,它是受检异常,你必须处理或声明它。

源码:

// Method.java
@CallerSensitive
public Object invoke(Object obj, Object... args)
    throws IllegalAccessException, IllegalArgumentException,
       InvocationTargetException
{
...
}

示例代码:

class StudentServiceImpl {

    public void say() {
        throw new IllegalArgumentException("Student say error");
    }
}

@Test
public void test1() {
    try {
        StudentServiceImpl studentService = new StudentServiceImpl();
        Method method = StudentServiceImpl.class.getMethod("say");
        /**
         * 抛出 InvocationTargetException 异常
         * @see sun.reflect.NativeMethodAccessorImpl#invoke0(Method, Object, Object[])
         */
        method.invoke(studentService);
    } catch (InvocationTargetException e) {
        // 获取原始异常
        Throwable originalException = e.getCause();
        e.printStackTrace();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

结果:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.example.MyTest1.test1(MyTest1.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:231)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: java.lang.IllegalArgumentException: Student say error
    at org.example.MyTest1$StudentServiceImpl.say(MyTest1.java:13)
    ... 32 more