AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-exception-handling: What is the reason behind InvocationTargetException?

Please explain the reason behind InvocationTargetException.

java x 210
exception-handling x 24
Posted On : 2016-05-14 23:49:37.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188221961
up-rate
5
down-rate

Answers


InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. The thrown exception is provided at construction time and can be accessed via the getTargetException method. That exception is known as the cause and can be accessed via the getCause method.

Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java Virtual Machine. The reflection layer wraps any thrown exception as an InvocationTargetException. In this way, it is clear whether the exception was actually caused by a failure in the reflection call, or a failure within the method called.

If you decide to use reflection, consider enclosing your code inside a try-catch statement and manipulate the InvocationTargetException accordingly. Notice that the result of the getCause method can be one of the following:
1. A null value.
2. An unchecked exception, such as RuntimeException, IllegalArgumentException, NullPointerException, etc.
3. A checked exception, such as NameNotFoundException, etc.
4. A java.lang.Error, such as StackOverflowError, OutOfMemoryError, etc.

In your application´s code, make sure that you check for all aforementioned cases, otherwise your code may produce undesired bugs.

The following code snippet throws an InvocationTargetException:

ReflectionExample.java:
----------------------------------------------------------
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionExample {

@SuppressWarnings("unused")
private int testMethod(String str) {
if(str.length() == 0)
throw new IllegalArgumentException("The string must contain at least one character!");

System.out.println("Inside testMethod: argument´s value equals to: \"" + str + "\"");

return 0;
}

public static void main(String... args) {
try {
// Retrieve an instance of the current class as an Object.
Class<?> c = Class.forName("main.java.ReflectionExample");
Object t = c.newInstance();

Method[] declaredMethods = c.getDeclaredMethods();
for (Method method : declaredMethods) {
String methodName = method.getName();

// Skip the current main method.
if(methodName.contains("main"))
continue;

System.out.format("Invoking %s()%n", methodName);
try {
// Declare the method as accessible.
method.setAccessible(true);

/* Invoke the method with a ´null´ parameter value, in order
* for an exception to be thrown. */
Object returnValue = method.invoke(t, "");

System.out.format("%s() returned: %d%n", methodName, returnValue);
}
catch (InvocationTargetException ex) {
System.err.println("An InvocationTargetException was caught!");
Throwable cause = ex.getCause();
System.out.format("Invocation of %s failed because of: %s%n",
methodName, cause.getMessage());
}
}
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
System.err.println("The following exception was thrown:");
ex.printStackTrace();
}
}
}


The result of the above snippet is:
-----------------------------------------------------------------------------------
Invoking testMethod()
An InvocationTargetException was caught!
Invocation of testMethod failed because of: The string must contain at least one character!
If we carefully observe the code, we will understand why the InvocationTargetException was thrown. Initially, we get an instance of the ReflectionExample class. Then, we iterate over its declared methods and we call the method under the name testMethod, passing an empty String as an argument.


However, the testMethod throws an IllegalArgumentException, in case the length of the string equals to zero. That exception is wrapped as an InvocationTargetException and is thrown in our sample application.

If we change the 39th line to:
Object returnValue = method.invoke(t, "Hello from Java Code Geeks!");
the execution continues without any exception being thrown. As a result, we get the following result:
Invoking testMethod()
Inside testMethod: argument´s value equals to: "Hello from Java Code Geeks!"
testMethod() returned: 0

Posted On : 2016-05-15 00:03:45
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129026019
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in