Core Java

java.lang.reflect.invocationtargetexception

1. Introduction

In this article, we will understand the meaning of the java.lang.reflect.invocationtargetexception, and why this exception is thrown.

Programs that need to be able to observe or alter the behavior of applications running inside the Java virtual machine frequently employ reflection. Developers with a firm command of the language’s foundations should be the only ones to use this feature because it is a relatively complex one. With that limitation in mind, a reflection is a strong tool that allows applications to execute tasks that would otherwise be impractical.

By establishing instances of extension objects with their fully qualified names, an application can utilize external, user-defined classes. Using the type of information provided by reflection in visual development environments can help developers write better code. Debuggers must be able to inspect private class members.

2. InvocationTargetException

This class extends ReflectiveOperationException, which is the common superclass of exceptions thrown by reflective operations in core reflection. InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Let us now create an example for it. We will create a very simple Application class with one method divide(). This method will take two parameters and will divide the first parameter by the second one:

public int divide (Integer a, Integer b) {
    return a/b;
}

Now in the main method, we will get the method object from the getMethod():

final Method method = Application.class.getMethod("divide", Integer.class, Integer.class);

Now invoke the method passing the parameters. Call the method with the second parameter as zero. This will result in ArithmeticException.

try {
        method.invoke(application, 1, 0);
    } catch (InvocationTargetException e) {
        e.printStackTrace();
}

Application.java

package org.javacodegeeks;

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

public class Application {

    public int divide (Integer a, Integer b) {
        return a/b;
    }

    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException {
        Application application = new Application();
        final Method method = Application.class.getMethod("divide", Integer.class, Integer.class);
        try {
            method.invoke(application, 1, 0);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

When you run the above class you will see an error as below. The division by zero will result in ArithmeticException which will be consumed by InvocationTargetException:

Fig. 1: java.lang.reflect.invocationtargetexception.
Fig. 1: java.lang.reflect.invocationtargetexception.

If you want to know the actual cause of the exception when the InvocationTargetException is thrown then you can get the cause from the InvocationTargetException object.

try {
    method.invoke(application, 1, 0);
} catch (InvocationTargetException e) {
    final Throwable cause = e.getCause();
    cause.printStackTrace();
}

Below is the output you will see:

Fig. 2: java.lang.reflect.invocationtargetexception.
Fig. 2: java.lang.reflect.invocationtargetexception.

3. Conclusion

In this article, we learned about InvocationTargetException. First, we briefly discussed the Reflection API in Java. We discussed the case when InvocationTargetException can be thrown and we also discussed how we get the actual cause of the exception.

4. Download

This was an example displaying where Java InvocationTargetException gets thrown and how to handle it.

Download
You can download the full source code of this example here: java.lang.reflect.invocationtargetexception

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button