您的位置:首页 > 其它

Method.invoke 异常捕获

2014-03-07 12:21 357 查看
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.

”,应该是因为反射执行的方法里抛出了异常造成的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: