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());//此處輸出為"程序錯(cuò)誤"
??throw?new?Exception(e.getCause());
}
Method.invoke()方法將程序異常終止轉(zhuǎn)化為InvocationTargetException,并將錯(cuò)誤信息存放在?e.getCause(),這樣可以在外部調(diào)用中捕獲異常信息拋出
?
http://www.au92.com/archives/java-reflect-method-catch-InvocationTargetException.html
公司項(xiàng)目中使用了Java的反射,通過反射執(zhí)行別人的方法時(shí)候拋出了一個(gè)“java.lang.reflect.InvocationTargetException”的異常,百度了一下看的云里霧里的。
查看Java手冊(cè)的解釋是:InvocationTargetException 是一種包裝由調(diào)用方法或構(gòu)造方法所拋出異常的經(jīng)過檢查的異常。依然是很難理解。
因?yàn)樯婕暗娇缃M調(diào)試等問題,只能繼續(xù)Google。最終還是在stackoverflow看到一個(gè)比較靠譜的解釋“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.
”,應(yīng)該是因?yàn)榉瓷鋱?zhí)行的方法里拋出了異常造成的。
?
總結(jié)
以上是生活随笔為你收集整理的Method.invoke 异常捕获的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于 Unloading class s
- 下一篇: svn switch 导致个别文件swi