java.lang.reflect.invocationtargetexception
2015-06-19 20:27
603 查看
bout Sotirios-Efstathios Maneas
Sotirios-Efstathios (Stathis) Maneas is a postgraduate student at the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. His main interests include distributed systems, web crawling, model checking, operating systems, programming languages and web applications.
java.lang.reflect.invocationtargetexception – How to handle Invocation Target Exception
Posted by: Sotirios-Efstathios Maneas in exceptions December 31st, 2013Reflection 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.
The
InvocationTargetExceptionis 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
getTargetExceptionmethod. That exception is known as the
causeand can be accessed via the
getCausemethod.
For more information about the reflection in Java, please refer to the page
here.
Error case
The following code snippet throws anInvocationTargetException:
ReflectionExample.java:
package main.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
InvocationTargetExceptionwas thrown. Initially, we get an instance of the
ReflectionExampleclass. 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
testMethodthrows an
IllegalArgumentException, in case the length of the string equals to zero. That exception is wrapped as an
InvocationTargetExceptionand 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
How to deal with the exception
First of all, coding an application using reflection is hard. A developer must have a strong grasp of the internal structure of the Java programming language, because the usage of reflection contains drawbacks and dangers, such as performance overhead and exposure of internal fields and methods.If you decide to use reflection, consider enclosing your code inside a try-catch statement and manipulate the
InvocationTargetExceptionaccordingly. Notice that the result of the
getCausemethod can be one of the following:
A null value.
An unchecked exception, such as
RuntimeException,
IllegalArgumentException,
NullPointerException, etc.
A checked exception, such as
NameNotFoundException, etc.
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.
This was a tutorial about Java’s
InvocationTargetException.
Tagged with: reflection
The essay comes from:http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-reflect-invocationtargetexception-how-to-handle-invocation-target-exception/
相关文章推荐
- JAVA反射机制入门
- java中table表格数据居中问题
- Java知多少(97)绘图模式概述
- 打开Eclipse遇到Failed to create the Java Virtual Machine问题
- java,可见性
- C#实现标准JAVA的Md5withRsa算法
- 在JAVA中目录列表器file的应用及Matcher对象与Pattern对象的使用
- Java引用类型具体解释
- Struts2开发步骤
- JAVA推箱子游戏(一)
- Java学习日记之掌控硬盘(1)
- Eclipse各种抓狂问题集锦(一)
- Java小白手记:SSH
- Java小白手记:SSH
- JAVA判断文件编码类型
- Java环境变量设置
- [置顶] java版多线程下载
- [置顶] 自动更具数据库表来生成.java文件的小工具
- [置顶] java值传递
- java bean和xml相互转换