Blog |

How to Resolve InvocationTargetException in Java

How to Resolve Invocation<wbr>Target<wbr>Exception in Java
Table of Contents

If a InvocationTargetException is a checked exception in Java that wraps an exception thrown by an invoked method or constructor. The method or constructor that throws the exception is invoked using the Method.invoke() method. The InvocationTargetException is quite common when using the Java Reflection API.

The Java reflection layer wraps any exception as an InvocationTargetException. This helps clarify whether the exception is caused by an issue in the reflection call or within the called method.

What Causes InvocationTargetException

The InvocationTargetException occurs mainly when working with the Java reflection API to invoke a method or constructor, which throws an exception.

This underlying exception is the actual cause of the issue, therefore resolving the InvocationTargetException equates to finding and resolving the underlying exception that occurs within the invoked method.

InvocationTargetException Example

Here is an example of a InvocationTargetException thrown when a method that is called using Method.invoke() throws an exception:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InvocationTargetExceptionExample {
    public int divideByZero() {
                return 1 / 0;
        }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
        InvocationTargetExceptionExample itee = new InvocationTargetExceptionExample(); 
        Method method = InvocationTargetExceptionExample.class.getMethod("divideByZero");
        try {
            method.invoke(itee);
        } catch (InvocationTargetException ite) {
            ite.printStackTrace();
        }
        }
}

In this example, the main() method invokes divideByZero() using Method.invoke(). Since divideByZero() throws an ArithmeticException, it is wrapped within an InvocationTargetException thrown in the main() 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)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at InvocationTargetExceptionExample.main(InvocationTargetExceptionExample.java:13)
Caused by: java.lang.ArithmeticException: / by zero
    at InvocationTargetExceptionExample.divideByZero(InvocationTargetExceptionExample.java:6)
    ... 5 more

How to Resolve InvocationTargetException

Since the underlying exception is the actual cause of the InvocationTargetException, finding and resolving the underlying exception resolves the InvocationTargetException. The getCause() method of the Throwable class can be used to obtain the underlying exception. The earlier example can be updated accordingly to get the underlying exception and print its stack trace:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class InvocationTargetExceptionExample {
    public int divideByZero() {
        return 1 / 0;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        InvocationTargetExceptionExample itee = new InvocationTargetExceptionExample();
        Method method = InvocationTargetExceptionExample.class.getMethod("divideByZero");
        try {
            method.invoke(itee);
        } catch (InvocationTargetException ite) {
            Throwable underlyingException = ite.getCause();
            underlyingException.printStackTrace();
        } catch (IllegalAccessException iae) {
            iae.printStackTrace();
        }
    }
}

Running the above will print out the stack trace of the underlying ArithmeticException:

java.lang.ArithmeticException: / by zero
    at InvocationTargetExceptionExample.divideByZero(InvocationTargetExceptionExample.java:6)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at InvocationTargetExceptionExample.main(InvocationTargetExceptionExample.java:13)

The stack trace can then be inspected to resolve the underlying exception. This will also resolve the wrapped InvocationTargetException.

Track, Analyze and Manage Java Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates Java error monitoring and triaging, making fixing errors easier than ever. Try it today.

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape