读书人

Java Dynamic Proxy: How to know wha

发布时间: 2013-01-23 10:44:50 作者: rapoo

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.") ;}}


读书人网 >编程

热点推荐