Method.invoke 异常捕获

http://bbs.csdn.net/topics/80291698

try{
  Method.invoke(BO,VO);
}catch(InvocationTargetException e) {
  System.out.println(e.getMessage());//此处输出为空
  System.out.println(e.getCause());//此处输出为"程序错误"
  throw new Exception(e.getCause());
}

Method.invoke()方法将程序异常终止转化为InvocationTargetException,并将错误信息存放在 e.getCause(),这样可以在外部调用中捕获异常信息抛出

 

http://www.au92.com/archives/java-reflect-method-catch-InvocationTargetException.html

公司项目中使用了Java的反射,通过反射执行别人的方法时候抛出了一个“java.lang.reflect.InvocationTargetException”的异常,百度了一下看的云里雾里的。

查看Java手册的解释是:InvocationTargetException 是一种包装由调用方法或构造方法所抛出异常的经过检查的异常。依然是很难理解。

因为涉及到跨组调试等问题,只能继续Google。最终还是在stackoverflow看到一个比较靠谱的解释“From the Javadoc of Method.invoke()


Throws: InvocationTargetException - if the underlying method throws an exception.


This exception is throw if the method called threw an exception.

”,应该是因为反射执行的方法里抛出了异常造成的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值