public class ExceptionUtil extends Object
Constructor and Description |
---|
ExceptionUtil() |
Modifier and Type | Method and Description |
---|---|
static <T extends Throwable> |
convertFromOrSuppressedThrowable(Throwable throwable,
Class<T> exceptionClass)
转化指定异常为来自或者包含指定异常
|
static <T extends Throwable> |
convertFromOrSuppressedThrowable(Throwable throwable,
Class<T> exceptionClass,
boolean checkCause)
转化指定异常为来自或者包含指定异常
|
static Throwable |
getCausedBy(Throwable throwable,
Class<? extends Exception>... causeClasses)
获取由指定异常类引起的异常
|
static String |
getMessage(Throwable e)
获得完整消息,包括异常名,消息格式为:{SimpleClassName}: {ThrowableMessage}
|
static Throwable |
getRootCause(Throwable throwable)
获取异常链中最尾端的异常,即异常最早发生的异常对象。
|
static String |
getRootCauseMessage(Throwable th)
获取异常链中最尾端的异常的消息,消息格式为:{SimpleClassName}: {ThrowableMessage}
|
static StackTraceElement |
getRootStackElement()
获取入口堆栈信息
|
static String |
getSimpleMessage(Throwable e)
获得消息,调用异常类的getMessage方法
|
static StackTraceElement |
getStackElement(int i)
获取指定层的堆栈信息
|
static StackTraceElement |
getStackElement(String fqcn,
int i)
获取指定层的堆栈信息
|
static StackTraceElement[] |
getStackElements()
获取当前栈信息
|
static List<Throwable> |
getThrowableList(Throwable throwable)
获取异常链上所有异常的集合,如果
Throwable 对象没有cause,返回只有一个节点的List如果传入null,返回空集合 此方法来自Apache-Commons-Lang3 |
static boolean |
isCausedBy(Throwable throwable,
Class<? extends Exception>... causeClasses)
判断是否由指定异常类引起
|
static boolean |
isFromOrSuppressedThrowable(Throwable throwable,
Class<? extends Throwable> exceptionClass)
判断指定异常是否来自或者包含指定异常
|
static boolean |
isFromOrSuppressedThrowable(Throwable throwable,
Class<? extends Throwable> exceptionClass,
boolean checkCause)
判断指定异常是否来自或者包含指定异常
|
static String |
stacktraceToOneLineString(Throwable throwable)
堆栈转为单行完整字符串
|
static String |
stacktraceToOneLineString(Throwable throwable,
int limit)
堆栈转为单行完整字符串
|
static String |
stacktraceToString(Throwable throwable)
堆栈转为完整字符串
|
static String |
stacktraceToString(Throwable throwable,
int limit)
堆栈转为完整字符串
|
static String |
stacktraceToString(Throwable throwable,
int limit,
Map<Character,String> replaceCharToStrMap)
堆栈转为完整字符串
|
static Throwable |
unwrap(Throwable wrapped)
剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常,返回业务本身的异常
|
static <T extends Throwable> |
wrap(Throwable throwable,
Class<T> wrapThrowable)
包装一个异常
|
static void |
wrapAndThrow(Throwable throwable)
|
static RuntimeException |
wrapRuntime(String message)
将指定的消息包装为运行时异常
|
static RuntimeException |
wrapRuntime(Throwable throwable)
使用运行时异常包装编译异常
如果传入参数已经是运行时异常,则直接返回,不再额外包装 |
static void |
wrapRuntimeAndThrow(String message)
将消息包装为运行时异常并抛出
|
public static String getMessage(Throwable e)
e
- 异常public static String getSimpleMessage(Throwable e)
e
- 异常public static RuntimeException wrapRuntime(Throwable throwable)
如果传入参数已经是运行时异常,则直接返回,不再额外包装
throwable
- 异常public static RuntimeException wrapRuntime(String message)
message
- 异常消息public static <T extends Throwable> T wrap(Throwable throwable, Class<T> wrapThrowable)
T
- 被包装的异常类型throwable
- 异常wrapThrowable
- 包装后的异常类public static void wrapAndThrow(Throwable throwable)
throwable
- 异常public static void wrapRuntimeAndThrow(String message)
message
- 异常消息public static Throwable unwrap(Throwable wrapped)
wrapped
- 包装的异常public static StackTraceElement[] getStackElements()
public static StackTraceElement getStackElement(int i)
i
- 层数public static StackTraceElement getStackElement(String fqcn, int i)
fqcn
- 指定类名为基础i
- 指定类名的类堆栈相对层数public static StackTraceElement getRootStackElement()
public static String stacktraceToOneLineString(Throwable throwable)
throwable
- 异常对象public static String stacktraceToOneLineString(Throwable throwable, int limit)
throwable
- 异常对象limit
- 限制最大长度public static String stacktraceToString(Throwable throwable)
throwable
- 异常对象public static String stacktraceToString(Throwable throwable, int limit)
throwable
- 异常对象limit
- 限制最大长度public static String stacktraceToString(Throwable throwable, int limit, Map<Character,String> replaceCharToStrMap)
throwable
- 异常对象limit
- 限制最大长度,<0表示不限制长度replaceCharToStrMap
- 替换字符为指定字符串public static boolean isCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
throwable
- 异常causeClasses
- 定义的引起异常的类public static Throwable getCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses)
throwable
- 异常causeClasses
- 定义的引起异常的类public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass)
throwable
- 异常exceptionClass
- 定义的引起异常的类public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass, boolean checkCause)
throwable
- 异常exceptionClass
- 定义的引起异常的类checkCause
- 判断causepublic static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass)
T
- 异常类型throwable
- 异常exceptionClass
- 定义的引起异常的类public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass, boolean checkCause)
T
- 异常类型throwable
- 异常exceptionClass
- 定义的引起异常的类checkCause
- 判断causepublic static List<Throwable> getThrowableList(Throwable throwable)
throwable
- 异常对象,可以为nullpublic static Throwable getRootCause(Throwable throwable)
Throwable.getCause()
直到没有cause为止,如果异常本身没有cause,返回异常本身此方法来自Apache-Commons-Lang3
throwable
- 异常对象,可能为nullCopyright © 2024. All rights reserved.