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.
”,应该是因为反射执行的方法里抛出了异常造成的。
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.
”,应该是因为反射执行的方法里抛出了异常造成的。
相关文章推荐
- 通过MethodInfo.Invoke调用时使用的异常捕捉
- Android异常:Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)'
- 无法捕获的异常:MissingMethodException
- 无法捕获的异常:MissingMethodException
- 动态代理之一:JDK动态代理 和异常 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j
- dubbo异常: Failed to invoke the method getFormulaZtree in the service 异常解决方案
- 动态代理之一:JDK动态代理 和异常 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
- Android异常 NullPointerException: Attempt to invoke virtual method int java.lang.Integer.intValue()
- 无法捕获的异常:MissingMethodException
- 子类通过父类的Method对象的invoke执行的还是自己的方法
- hibernate unique配置 和 异常的捕获
- Android 捕获全局异常信息并进行相关处理
- IOS程序异常crash捕获与拦截
- android.view.InflateException: Binary XML file line #121: Attempt to invoke virtual method
- NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.la
- Android 全局捕获异常
- 【Java笔记】异常处理:捕获和抛出基础
- 关于构造中new的异常捕获!
- iOS崩溃 捕获异常处理(二)
- 多线程编程注意捕获异常