一、前言
关于tomcat源码包中定义org.apache.tomcat.util.ExceptionUtils异常工具类,可以处理除虚拟机错误VirtualMachineError和线程死亡ThreadDeath情况外一切可抛出Throwable异常,并对类反射方法调用异常InvocationTargetException进行包装自定义处理,详情参见源码示例说明。
二、源码说明
package org.apache.tomcat.util;@b@@b@import java.lang.reflect.InvocationTargetException;@b@@b@@b@/**@b@ * Utilities for handling Throwables and Exceptions.@b@ */@b@public class ExceptionUtils {@b@@b@ /**@b@ * Checks whether the supplied Throwable is one that needs to be@b@ * rethrown and swallows all others.@b@ * @param t the Throwable to check@b@ */@b@ public static void handleThrowable(Throwable t) {@b@ if (t instanceof ThreadDeath) {@b@ throw (ThreadDeath) t;@b@ }@b@ if (t instanceof VirtualMachineError) {@b@ throw (VirtualMachineError) t;@b@ }@b@ // All other instances of Throwable will be silently swallowed@b@ }@b@@b@ /**@b@ * Checks whether the supplied Throwable is an instance of@b@ * <code>InvocationTargetException</code> and returns the throwable that is@b@ * wrapped by it, if there is any.@b@ *@b@ * @param t the Throwable to check@b@ * @return <code>t</code> or <code>t.getCause()</code>@b@ */@b@ public static Throwable unwrapInvocationTargetException(Throwable t) {@b@ if (t instanceof InvocationTargetException && t.getCause() != null) {@b@ return t.getCause();@b@ }@b@ return t;@b@ }@b@}