InvocationTargetException 浅析
2015-07-08 16:18
330 查看
转自:http://blog.csdn.net/simpleit/article/details/5415066
InvocationTargetException 异常是在 java.lang.reflect 包中, 所以该异常的产生和反射有关.
[java] view
plaincopy
package org.simpleit.test;
public class Calculator {
public int div(int i, int j){
return i / j;
}
}
[java] view
plaincopy
package org.simpleit.test;
public class TestCalculator {
public void testDiv(Calculator calculator, Integer a, Integer b){
System.out.println(calculator.div(a, b));
}
}
[java] view
plaincopy
@SuppressWarnings("unchecked")
@Test
public void testInvocationTargetException() throws Exception{
TestCalculator testCalculator = new TestCalculator();
Calculator calculator = new Calculator();
Class clazz = TestCalculator.class;
Method testDiv = clazz.getDeclaredMethod("testDiv", Calculator.class,
Integer.class, Integer.class);
//以下代码会抛出 InvocationTargetException 异常
testDiv.invoke(testCalculator, calculator, 12, 0);
}
testDiv.invoke(testCalculator, calculator, 12, 0); 行代码会抛出 InvocationTargetException 异常, 但若直接
testCalculator.testDiv(calculator, 12, 0); 则抛出的却是 java.lang.ArithmeticException 异常.
结论: 在利用 Method 对象的 invoke 方法调用目标对象的方法时, 若在目标对象的方法内部抛出异常, 会抛出 InvocationTargetException 异常, 该异常包装了目标对象的方法内部抛出异常, 可以通过调用 InvocationTargetException 异常类的的 getTargetException() 方法得到原始的异常.
InvocationTargetException 异常是在 java.lang.reflect 包中, 所以该异常的产生和反射有关.
[java] view
plaincopy
package org.simpleit.test;
public class Calculator {
public int div(int i, int j){
return i / j;
}
}
[java] view
plaincopy
package org.simpleit.test;
public class TestCalculator {
public void testDiv(Calculator calculator, Integer a, Integer b){
System.out.println(calculator.div(a, b));
}
}
[java] view
plaincopy
@SuppressWarnings("unchecked")
@Test
public void testInvocationTargetException() throws Exception{
TestCalculator testCalculator = new TestCalculator();
Calculator calculator = new Calculator();
Class clazz = TestCalculator.class;
Method testDiv = clazz.getDeclaredMethod("testDiv", Calculator.class,
Integer.class, Integer.class);
//以下代码会抛出 InvocationTargetException 异常
testDiv.invoke(testCalculator, calculator, 12, 0);
}
testDiv.invoke(testCalculator, calculator, 12, 0); 行代码会抛出 InvocationTargetException 异常, 但若直接
testCalculator.testDiv(calculator, 12, 0); 则抛出的却是 java.lang.ArithmeticException 异常.
结论: 在利用 Method 对象的 invoke 方法调用目标对象的方法时, 若在目标对象的方法内部抛出异常, 会抛出 InvocationTargetException 异常, 该异常包装了目标对象的方法内部抛出异常, 可以通过调用 InvocationTargetException 异常类的的 getTargetException() 方法得到原始的异常.
相关文章推荐
- android.os.TransactionTooLargeException
- 导出文件,如果文件名重复则在后面加上(数字)--> xxxx(2).xxx
- c++ 时间类型详解 time_t
- LeetCode144 Binary Tree PreOrder Traversal Java题解(递归 迭代)
- 黑马程序员---正则表达式
- Dynamics AX 2012 R2 IIS WebSite Unauthorized 401
- wireshark插件编程总结
- 看一眼1分钟正则实例带你玩转正则表达式!
- ASP.Net操作Access数据库总结
- ASE 登录 报错Msg 4002, Level 14, State 1:Login failed. CT-LIBRARY error: ct_connect(): protocol speci
- 设计模式之禅之模版方法模式
- python捕捉对象(异常处理)
- [Android] 判断手机上是否安装了某个程序
- 横标和总表的概念
- DCT离散余弦变换C版本
- java学习笔记 ---数组
- C#如何打印条形码EAN13码
- 论Linux运维的一些基础安全知识和简单办法
- 工作日志 7.8
- centos创建新用户