public class ExceptionKits extends Object
1. Checked/Uncheked及Wrap(如ExecutionException)的转换.
2. 打印Exception的辅助函数. (其中一些来自Common Lang ExceptionUtils)
3. 查找Cause(其中一些来自Guava Throwables)
4. StackTrace性能优化相关,尽量使用静态异常避免异常生成时获取StackTrace(Netty)
copy from vipshop VJTools(com.vip.vjtools.vjkit.base.ExceptionUtil) and made some changes.
| 构造器和说明 |
|---|
ExceptionKits() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T extends Throwable> |
clearStackTrace(T throwable)
清除StackTrace.
|
static <T extends Throwable> |
findCause(Throwable throwable,
Class<T> cause)
获取某种类型的cause,如果没有则返回空
copy from Jodd ExceptionKits
|
static Throwable |
getRootCause(Throwable t)
获取异常的Root Cause.
|
static boolean |
isCausedBy(Throwable throwable,
Class<? extends Exception>... causeExceptionClasses)
判断异常是否由某些底层的异常引起.
|
static <T extends Throwable> |
setStackTrace(T throwable,
Class<?> throwClass,
String throwClazz)
copy from Netty, 为静态异常设置StackTrace.
|
static String |
stackTraceText(Throwable t)
将StackTrace[]转换为String, 供Logger或e.printStackTrace()外的其他地方使用.
|
static String |
toStringWithRootCause(Throwable t)
拼装 短异常类名: 异常信息 <-- RootCause的短异常类名: 异常信息
|
static String |
toStringWithShortName(Throwable t)
拼装 短异常类名: 异常信息.
|
static RuntimeException |
unchecked(Throwable t)
将CheckedException转换为RuntimeException重新抛出, 可以减少函数签名中的CheckExcetpion定义.
|
static Throwable |
unwrap(Throwable t)
如果是著名的包裹类,从cause中获得真正异常.
|
static RuntimeException |
unwrapAndUnchecked(Throwable t)
组合unwrap与unchecked,用于处理反射/Callable的异常
|
public static RuntimeException unchecked(Throwable t)
CheckedException会用UndeclaredThrowableException包裹,RunTimeException和Error则不会被转变.
copy from Commons Lange 3.5 ExceptionUtils.
虽然unchecked()里已直接抛出异常,但仍然定义返回值,方便欺骗Sonar。因此本函数也改变了一下返回值
示例代码:
try{ ... }catch(Exception e){ throw unchecked(t); }
ExceptionUtils.wrapAndThrow(Throwable)public static Throwable unwrap(Throwable t)
Future中使用的ExecutionException 与 反射时定义的InvocationTargetException, 真正的异常都封装在Cause中
前面 unchecked() 使用的UncheckedException同理.
public static RuntimeException unwrapAndUnchecked(Throwable t)
public static String stackTraceText(Throwable t)
为了使用StringBuilderWriter,没有用Throwables#getStackTraceAsString(Throwable)
public static String toStringWithShortName(Throwable t)
与Throwable.toString()相比使用了短类名
ExceptionUtils.getMessage(Throwable)public static String toStringWithRootCause(Throwable t)
public static Throwable getRootCause(Throwable t)
如无底层Cause, 则返回自身
Throwables.getRootCause(Throwable)public static <T extends Throwable> T findCause(Throwable throwable, Class<T> cause)
copy from Jodd ExceptionKits
public static boolean isCausedBy(Throwable throwable, Class<? extends Exception>... causeExceptionClasses)
public static <T extends Throwable> T setStackTrace(T throwable, Class<?> throwClass, String throwClazz)
对某些已知且经常抛出的异常, 不需要每次创建异常类并很消耗性能的并生成完整的StackTrace. 此时可使用静态声明的异常.
如果异常可能在多个地方抛出,使用本函数设置抛出的类名和方法名.
private static RuntimeException TIMEOUT_EXCEPTION = ExceptionKits.setStackTrace(new RuntimeException("Timeout"),
MyClass.class, "mymethod");
public static <T extends Throwable> T clearStackTrace(T throwable)
如果不能控制StackTrace的生成,也不能控制它的打印端(如logger),可用此方法暴力清除Trace.
但Cause链依然不能清除, 只能清除每一个Cause的StackTrace.
Copyright © 2019. All rights reserved.