/** * @author xichao.dong * @email 406592176@qq.com * @date 2014-8-15 上午8:50:38 * @company (开发公司) 珠海市冰川软件有限公司 * @copyright (版权) 本文件归属珠海市冰川软件有限公司所有 * @version V1.0 * @modify (修改) : 2014-8-15 上午8:50:38 xichao.dong * @Review (审核人) :xichao.dong */ package com.glacier.basic.util; import java.io.PrintWriter; import java.io.StringWriter; /** * @ClassName: Exceptions * @Description: TODO(关于异常的工具类) * @author xichao.dong * @email 406592176@qq.com * @date 2014-8-15 上午11:14:52 */ public class Exceptions { /** * 将CheckedException转换为UncheckedException. */ public static RuntimeException unchecked(Exception e) { if (e instanceof RuntimeException) { return (RuntimeException) e; } else { return new RuntimeException(e); } } /** * 将ErrorStack转化为String. */ public static String getStackTraceAsString(Exception e) { StringWriter stringWriter = new StringWriter(); e.printStackTrace(new PrintWriter(stringWriter)); return stringWriter.toString(); } /** * 判断异常是否由某些底层的异常引起. */ public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) { Throwable cause = ex.getCause(); while (cause != null) { for (Class<? extends Exception> causeClass : causeExceptionClasses) { if (causeClass.isInstance(cause)) { return true; } } cause = cause.getCause(); } return false; } }