首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java Dynamic Proxy: How to know what exception cause java.lang.reflect.Invocatio

2013-01-23 
Java Dynamic Proxy: How to know what exception cause java.lang.reflect.InvocationTargetException?Is

Java Dynamic Proxy: How to know what exception cause java.lang.reflect.InvocationTargetException?

Issue:

When we use Java Dynamic Proxy, method.invoke() is a necessary step to call the real method we want. But once this method triggers an exception during execution, the final exception printed out is always "java.lang.reflect.InvocationTargetException". So, how can we know that which exact exception caused JVM throw InvocationTargetException?

 

Solution:

We can use e.getCause() to get this info, such as

 

Issue:When we use Java Dynamic Proxy, method.invoke() is a necessary step to call the real method we want. But once this method triggers an exception during execution, the final exception printed out is always "java.lang.reflect.InvocationTargetException". So, how can we know that which exact exception caused JVM throw InvocationTargetException? Solution:We can use e.getCause() to get this info, such as public Object invoke(Object proxy, Method method, Object[] obj) throws Throwable {        Object result = null;        try {            result = method.invoke(delegation, obj);        } catch (Exception e) {           matchException(e) ;        }        return result;    }private void matchException(Exception e) {System.out.println(e.getCause()) ;if(e.getCause() instanceof SubException){System.out.println("This is SubException.") ;}else if(e.getCause() instanceof BaseException){System.out.println("This is BaseException.") ;}}


 

热点排行