java.lang.reflect.invocationtargetexception

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, 2013

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.

The 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.

For more information about the reflection in Java, please refer to the page here.

Error case

The following code snippet throws an InvocationTargetException:

ReflectionExample.java:

 1 package main.java;
 2
 3 import java.lang.reflect.InvocationTargetException;
 4
 5 import java.lang.reflect.Method;
 6
 7
 8 public class ReflectionExample {
 9
10  @SuppressWarnings("unused")
11         private int testMethod(String str) {
12             if(str.length() == 0)
13                 throw new IllegalArgumentException("The string must contain at least one character!");
14             System.out.println("Inside testMethod: argument‘s value equals to: \"" + str + "\"");
15             return 0;
16     }
17     public static void main(String... args) {
18             try {
19
20                 // Retrieve an instance of the current class as an Object.
21                 Class<?> c = Class.forName("main.java.ReflectionExample");
22
23                 Object t = c.newInstance();
24
25                 Method[] declaredMethods = c.getDeclaredMethods();
26
27                 for (Method method : declaredMethods) {
28
29                     String methodName = method.getName();
30
31                 // Skip the current main method.
32
33                     if(methodName.contains("main"))
34
35                         continue;
36
37                     System.out.format("Invoking %s()%n", methodName);
38
39                     try {
40
41                         // Declare the method as accessible.
42
43                         method.setAccessible(true);
44
45
46
47                         /* Invoke the method with a ‘null‘ parameter value, in order
48
49                          * for an exception to be thrown. */
50
51                         Object returnValue = method.invoke(t, "");
52
53
54
55                         System.out.format("%s() returned: %d%n", methodName, returnValue);
56
57                     }
58
59                     catch (InvocationTargetException ex) {
60
61                         System.err.println("An InvocationTargetException was caught!");
62
63                         Throwable cause = ex.getCause();
64
65                     System.out.format("Invocation of %s failed because of: %s%n",
66
67                                 methodName, cause.getMessage());
68
69                     }
70
71                 }
72
73             }
74             catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
75
76                 System.err.println("The following exception was thrown:");
77
78                 ex.printStackTrace();
79
80             }
81
82         }
83
84     }

The result of the above snippet is:

1 Invoking testMethod()

2 An InvocationTargetException was caught!

3 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:

1 Invoking testMethod()
2     Inside testMethod: argument‘s value equals to: "Hello from Java Code Geeks!"
3
4     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 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.

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/

时间: 2024-10-17 13:18:36

java.lang.reflect.invocationtargetexception的相关文章

org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException异常解决

org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException org.apache.struts2.json.JSONWriter.bean(JSONWriter.java:246) org.apache.struts2.json.JSONWriter.processCustom(JSONWriter.java:178) org.apache.struts2.json.JSONWriter.p

含有Date类型属性的实体类转化为JSONArray时报net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

当我们通过session传递数据的时候我通常是: 直接在DAO里从数据库取出含Date类型的数据,而且通常不会将java.sql.Date转为java.util.Date. 这时候前台和后台都不会有任何问题,所以我们以为这样是正常的操作. 但是当我们用JSON传递数据的时候就会发现控制台会报一下错误信息: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 这时候我们可以再DAO里将java.sql.Dat

session.createQuery()不执行和java.lang.reflect.InvocationTargetException

今天写SSH的工程的时候,执行到一个DAO中的Query query = session.createQuery(hql)的时候,没有成功执行,直接跳到了finally,然后前台报了500和java.lang.reflect.InvocationTargetException的错误. 代码如下: package dao; import java.util.List; import model.AccountInfo; import org.hibernate.HibernateException

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

注:在项目中, 我使用原生的ajax请求数据的时候, JSONObject没能帮我解析, 当却不给我报错, 我是在junit单元测试中测试的时候, 发现的.发现好多时候, 特别是通过ajax请求, 不给我们报错,很郁闷, 特别是ie, 有些问题, 得借助FireFox的返回结果分析. 当然, FireFox有时也没报错. 异常栈: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException at net.sf.js

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

1 //存储Product对象的集合是从,Product是从mysql数据库中查询并添加的 2 ArrayList<Product> list = new ArrayList<Product>(); 3 4 //设置响应对象编码 5 response.setCharacterEncoding("utf-8"); 6 response.setContentType("text/html;charset=utf-8"); 7 8 //将list集

HTTP Status 500 - java.lang.reflect.InvocationTargetException

type Exception report message java.lang.reflect.InvocationTargetException description The server encountered an internal error that prevented it from fulfilling this request. exception java.lang.RuntimeException: java.lang.reflect.InvocationTargetExc

错误: java.lang.reflect.InvocationTargetException

错误: java.lang.reflect.InvocationTargetException    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause

ajax请求json数据异常:nested exception is net.sf.json.JSONException: java.lang.reflect.InvocationTargetException] with root cause 1.异常原因:所请求的json数据中包含java.util.date数据类型,但是在后台并没有将其格式转换 2.解决方法:添加工具类DateJsonValueProcessor import java.text.SimpleDateFormat; imp

mavne install 报错org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException

maven install 报错 org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: nulljava.lang.reflect.InvocationTargetException at sun.reflect.