Signup/Sign In

Java InvocationTargetException

The InvocationTargetException is a checked exception that occurs when working with the reflection layer in Java. The Reflection API is used to examine or inspect the classes, fields, methods, and interfaces at runtime in Java.

It let us analyze the behavior of classes and their methods, even when we don't know their names. The InvocationTargetException is a commonly encountered error that occurs when using reflection.

In this tutorial, we will learn more about this error and how to resolve it.

Causes of InvocationTargetException in Java

The InvocationTargetException occurs when we call or invoke a method that returns an exception. The actual exception returned by the invoked method is wrapped by the InvocationTargetException.

Consider the following example where we have a class called Sample. We have a method inside this class that will always return an ArrayIndexOutOfBounds exception. When we invoke this method by using the invoke() method, then we will get the 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

The original exception is not shown to the user so that the user can tell whether the error occurred due to invoking the method through reflection or because of some other issue in our current method. For example, if we had an array of size 10 in the main() method and we were trying to access its 11th element, then we would get an ArrayIndexOutOfBounds exception. Now, if the invoked method also showed the same ArrayIndexOutOfBounds exception, then it will be difficult to understand the origin of the exception.

Resolving the InvocationTargetException in Java

We can easily resolve this exception if we know the actual cause of the exception in the invoked method. We can use the getCause() method on the exception and that will return the underlying exception in the invoked method. Let's use this method in our previous example and see the output.

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: Index 11 out of bounds for length 10

In some rare cases, we may need to use the getCause() method multiple times. For example, if a class method is itself using reflection and invoking some other class's method, and we are trying to invoke this method of the class then we need to use the getCause() method twice. This scenario is explained by the following code.

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: Index 11 out of bounds for length 10

Summary

The InvocationTargetException is a common exception that can occur when using the Reflection API. It occurs because the invoke() was called on a method that returns some exception. The underlying exception of the invoked method is wrapped by the InvocationTargetException. We can easily view the underlying error by using the getCause() method.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.