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.
”,應該是因為反射執行的方法里拋出了異常造成的。
?
總結
以上是生活随笔為你收集整理的Method.invoke 异常捕获的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于 Unloading class s
- 下一篇: svn switch 导致个别文件swi