Java InvocationTargetException

原文:https://www.studytonight.com/java-examples/java-invocationtargetexception

InvocationTargetException 是在 Java 中使用反射层时出现的选中异常。反射 API 用于在运行时检查 Java 中的类、字段、方法和接口。

它让我们分析类及其方法的行为,即使我们不知道它们的名称。InvocationTargetException是使用反射时经常遇到的错误。

在本教程中,我们将了解有关此错误以及如何解决它的更多信息。

Java 中出现InvocationTargetException的原因

当我们调用或调用一个返回异常的方法时,就会出现InvocationTargetException。被调用方法返回的实际异常由 InvocationTargetException 包装。

考虑下面的例子,其中我们有一个名为 Sample 的类。我们在这个类中有一个总是返回ArrayIndexOutOfBounds异常的方法。当我们使用**invoke()**方法调用这个方法时,我们将得到InvocationTargetException

import java.lang.reflect.Method;
class Sample
{
    public void indexOutOfBoundsMethod()
    {
        int[] arr = new int[10];
        System.out.print(arr[11]);
    }
}
public class InvocationException
{
    public static void main(String[] args) 
    {
        try
        {
            Sample s = new Sample();  
            Method m = Sample.class.getMethod("indexOutOfBoundsMethod");
            m.invoke(s);
        }
        catch(Exception e)
        {
            System.out.print(e);
        }
    }
}

Java . lang . reflect . invocationtargetexception

最初的异常没有显示给用户,这样用户就可以知道错误是由于通过反射调用方法还是由于我们当前方法中的一些其他问题而发生的。例如,如果我们在 main()方法中有一个大小为 10 的数组,并且我们试图访问它的第 11 个元素,那么我们会得到一个 ArrayIndexOutOfBounds 异常。现在,如果被调用的方法也显示了相同的 ArrayIndexOutOfBounds 异常,那么将很难理解异常的来源。

解析 Java 中的InvocationTargetException

如果我们知道被调用方法中异常的实际原因,我们可以很容易地解决这个异常。我们可以对异常使用**getCause()**方法,这将在调用的方法中返回底层异常。让我们在前面的例子中使用这个方法,并查看输出。

import java.lang.reflect.Method;
class Sample
{
    public void indexOutOfBoundsMethod()
    {
        int[] arr = new int[10];
        System.out.print(arr[11]);
    }
}
public class demo
{
    public static void main(String[] args) 
    {
        try
        {
            Sample s = new Sample();  
            Method m = Sample.class.getMethod("indexOutOfBoundsMethod");
            m.invoke(s);
        }
        catch(Exception e)
        {
            System.out.print(e.getCause());
        }
    }
}

Java . lang . arrayindexoutofboundsexception:长度为 10 的索引 11 超出范围

在一些罕见的情况下,我们可能需要多次使用getCause() 方法。例如,如果一个类方法本身使用反射并调用其他类的方法,而我们试图调用该类的这个方法,那么我们需要使用getCause()方法两次。下面的代码解释了这种情况。

import java.lang.reflect.Method;
class Sample1
{
    public void indexOutOfBounds()//Method that always throws IndexOutOfBounds
    {
        int[] arr = new int[10];
        System.out.print(arr[11]);
    }
}
class Sample2
{
    public void invokeIndexOutOfBounds() throws Exception
    {
        try
        {
            Sample1 s = new Sample1();  
            Method m = Sample1.class.getMethod("indexOutOfBounds");
            m.invoke(s);//Invoking the method with the exception
        }
        catch(Exception e)
        {
            throw e;//this Exception e is itself an InvocationTargetException
        }
    }
}
public class demo
{
    public static void main(String[] args) 
    {
        try
        {
            Sample2 s = new Sample2();  
            Method m = Sample2.class.getMethod("invokeIndexOutOfBounds");
            m.invoke(s);//invokeIndexOutOfBounds() throws the InvocationTargetException
        }
        catch(Exception e)
        {
            System.out.println(e);//Exception of the main() method
            System.out.println(e.getCause());//Exception of the invokeIndexOutOfBounds() method
            System.out.println(e.getCause().getCause());//Exception of the indexOutOfBounds() method
        }
    }
}

Java . lang . reflect . invocationtargetexception Java . lang . reflect . invocationtargetexception Java . lang . arrayindexoutofboundsexception:索引 11 超出长度 10 的界限

摘要

InvocationTargetException是使用反射 API 时可能出现的常见异常。发生这种情况是因为invoke() 是在返回异常的方法上调用的。被调用方法的基础异常由 InvocationTargetException 包装。我们可以通过使用getCause()方法轻松查看潜在的错误。