exceptionutils.java

来自「《j2ee开发全程实录》随书源码」· Java 代码 · 共 50 行

JAVA
50
字号
package com.cownew.ctk.common;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;

import org.apache.log4j.Logger;

public class ExceptionUtils
{
	/**
	 * convert Throwable to CTKRunTimeException
	 * 
	 * @param e
	 * @return
	 */
	public static CTKRunTimeException toRuntimeException(Throwable e)
	{
		CTKRunTimeException re = new CTKRunTimeException(e);
		re.setStackTrace(e.getStackTrace());
		Logger.getLogger(ExceptionUtils.class).error(e.getMessage(),e);
		return re;
	}

	/**
	 * 得到真正的异常(脱掉InvocationTargetException UndeclaredThrowableException 
	 *CTKRunTimeException等)
	 * @param t
	 * @return
	 */
	public static Throwable getRealThrowable(Throwable t)
	{
		Throwable realT = t;
		if (t instanceof InvocationTargetException)
		{
			Throwable targetException = ((InvocationTargetException) t)
					.getTargetException();
			realT = getRealThrowable(targetException);
		} else if (t instanceof UndeclaredThrowableException)
		{
			UndeclaredThrowableException unException = (UndeclaredThrowableException) t;
			realT = getRealThrowable(unException.getUndeclaredThrowable());
		}else if (t instanceof CTKRunTimeException)
		{
			CTKRunTimeException re = (CTKRunTimeException) t;
			realT = getRealThrowable(re.getCause());
		}
		return realT;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?