我们在使用各种RPC框架的时候经常会遇到UndeclaredThrowableException的情况,一般这种底层是使用了jdk的动态代理。
为什么会产生UndeclaredThrowableException异常呢?通过查阅javadoc官方文档(链接地址:https://docs.oracle.com/javase/6/docs/api/java/lang/reflect/InvocationHandler.html),发现了该异常抛出的条件,如下图:
上述原因翻译成关键一句就是:如果抛出的异常是检查型异常,而代理类在处理异常时没有发现该类型的异常在接口中声明,简单来说就是代理实例调用invocationhandler的invoke方法时,抛出检查型异常这个检查型异常不属于RuntimeException和Error,也没有在throws中定义,所以归到Throwable,这也是UndeclaredThrowableException中的undeclared和throwable的由来。UndeclaredThrowableExcepton是个非检查型异常,但是它包装了这个检查型异常,通过getUndeclaredThrowable()可以获取检查型异常。
下面结合代码给大家讲述一下,动态代理之后会生成$Proxy0.class文件,$Proxy0.class 文件内容说明了如何抛出了UndeclaredThrowableException异常,获取代理类文件的方法参考:https://www.jianshu.com/p/83a635eb93dd
被代理的类如下,方法签名没有抛出任何异常,生成的代理类在下面
public class TestInterfaceImpl implements TestInterface {
@Override
public void testFunction() {
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class Proxy0 extends Proxy implements TestInterface {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public Proxy0(InvocationHandler paramInvocationHandler) {
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject) {
try {
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final String toString() {
try {
return (String)this.h.invoke(this, m2, null);
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void testFunction() {
try {
this.h.invoke(this, m3, null);
return;
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final int hashCode() {
try {
return ((Integer)this.h.invoke(this, m0, null)).intValue();
} catch (Error|RuntimeException error) {
throw null;
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("com.sankuai.powerbank.collaboration.TestInterface").getMethod("testFunction", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
} catch (NoSuchMethodException noSuchMethodException) {
throw new NoSuchMethodError(noSuchMethodException.getMessage());
} catch (ClassNotFoundException classNotFoundException) {
throw new NoClassDefFoundError(classNotFoundException.getMessage());
}
}
}
可以看到在testFunction的try块中第一个catch中处理了Error和RuntimeException,而其他的异常,即catched exception都被下面的catch捕捉,包装成了UndeclaredThrowableException。
弄明白了UndeclaredThrowableException的含义,就可以下手改程序了。既然出现了异常,就得记下异常信息,由于原始异常在UndeclaredThrowableException中,所以使用getCause()或getUndeclaredThrowable()方法获取原始异常,然后记录在log中