Package com.luna.common.exception
Class ExceptionUtil
java.lang.Object
com.luna.common.exception.ExceptionUtil
异常工具类
- Author:
- Looly
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Throwable>
TconvertFromOrSuppressedThrowable
(Throwable throwable, Class<T> exceptionClass) 转化指定异常为来自或者包含指定异常static <T extends Throwable>
TconvertFromOrSuppressedThrowable
(Throwable throwable, Class<T> exceptionClass, boolean checkCause) 转化指定异常为来自或者包含指定异常static Throwable
getCausedBy
(Throwable throwable, Class<? extends Exception>... causeClasses) 获取由指定异常类引起的异常static String
获得完整消息,包括异常名,消息格式为:{SimpleClassName}: {ThrowableMessage}static Throwable
getRootCause
(Throwable throwable) 获取异常链中最尾端的异常,即异常最早发生的异常对象。
此方法通过调用Throwable.getCause()
直到没有cause为止,如果异常本身没有cause,返回异常本身
传入null返回也为nullstatic String
获取异常链中最尾端的异常的消息,消息格式为:{SimpleClassName}: {ThrowableMessage}static StackTraceElement
获取入口堆栈信息static String
获得消息,调用异常类的getMessage方法static StackTraceElement
getStackElement
(int i) 获取指定层的堆栈信息static StackTraceElement[]
获取当前栈信息getThrowableList
(Throwable throwable) 获取异常链上所有异常的集合,如果Throwable
对象没有cause,返回只有一个节点的List
如果传入null,返回空集合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
剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常,返回业务本身的异常static void
wrapAndThrow
(Throwable throwable) static RuntimeException
wrapRuntime
(String message) 将指定的消息包装为运行时异常static RuntimeException
wrapRuntime
(Throwable throwable) 使用运行时异常包装编译异常static void
wrapRuntimeAndThrow
(String message) 将消息包装为运行时异常并抛出
-
Constructor Details
-
ExceptionUtil
public ExceptionUtil()
-
-
Method Details
-
getMessage
获得完整消息,包括异常名,消息格式为:{SimpleClassName}: {ThrowableMessage}- Parameters:
e
- 异常- Returns:
- 完整消息
-
getSimpleMessage
获得消息,调用异常类的getMessage方法- Parameters:
e
- 异常- Returns:
- 消息
-
wrapRuntime
使用运行时异常包装编译异常
如果传入参数已经是运行时异常,则直接返回,不再额外包装
- Parameters:
throwable
- 异常- Returns:
- 运行时异常
-
wrapRuntime
将指定的消息包装为运行时异常- Parameters:
message
- 异常消息- Returns:
- 运行时异常
- Since:
- 5.5.2
-
wrapAndThrow
- Parameters:
throwable
- 异常
-
wrapRuntimeAndThrow
将消息包装为运行时异常并抛出- Parameters:
message
- 异常消息- Since:
- 5.5.2
-
unwrap
剥离反射引发的InvocationTargetException、UndeclaredThrowableException中间异常,返回业务本身的异常- Parameters:
wrapped
- 包装的异常- Returns:
- 剥离后的异常
-
getStackElements
获取当前栈信息- Returns:
- 当前栈信息
-
getStackElement
获取指定层的堆栈信息- Parameters:
i
- 层数- Returns:
- 指定层的堆栈信息
- Since:
- 4.1.4
-
getRootStackElement
获取入口堆栈信息- Returns:
- 入口堆栈信息
- Since:
- 4.1.4
-
stacktraceToOneLineString
堆栈转为单行完整字符串- Parameters:
throwable
- 异常对象- Returns:
- 堆栈转为的字符串
-
stacktraceToOneLineString
堆栈转为单行完整字符串- Parameters:
throwable
- 异常对象limit
- 限制最大长度- Returns:
- 堆栈转为的字符串
-
stacktraceToString
堆栈转为完整字符串- Parameters:
throwable
- 异常对象- Returns:
- 堆栈转为的字符串
-
stacktraceToString
堆栈转为完整字符串- Parameters:
throwable
- 异常对象limit
- 限制最大长度- Returns:
- 堆栈转为的字符串
-
stacktraceToString
public static String stacktraceToString(Throwable throwable, int limit, Map<Character, String> replaceCharToStrMap) 堆栈转为完整字符串- Parameters:
throwable
- 异常对象limit
- 限制最大长度replaceCharToStrMap
- 替换字符为指定字符串- Returns:
- 堆栈转为的字符串
-
isCausedBy
判断是否由指定异常类引起- Parameters:
throwable
- 异常causeClasses
- 定义的引起异常的类- Returns:
- 是否由指定异常类引起
- Since:
- 4.1.13
-
getCausedBy
public static Throwable getCausedBy(Throwable throwable, Class<? extends Exception>... causeClasses) 获取由指定异常类引起的异常- Parameters:
throwable
- 异常causeClasses
- 定义的引起异常的类- Returns:
- 是否由指定异常类引起
- Since:
- 4.1.13
-
isFromOrSuppressedThrowable
public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass) 判断指定异常是否来自或者包含指定异常- Parameters:
throwable
- 异常exceptionClass
- 定义的引起异常的类- Returns:
- true 来自或者包含
- Since:
- 4.3.2
-
isFromOrSuppressedThrowable
public static boolean isFromOrSuppressedThrowable(Throwable throwable, Class<? extends Throwable> exceptionClass, boolean checkCause) 判断指定异常是否来自或者包含指定异常- Parameters:
throwable
- 异常exceptionClass
- 定义的引起异常的类checkCause
- 判断cause- Returns:
- true 来自或者包含
- Since:
- 4.4.1
-
convertFromOrSuppressedThrowable
public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass) 转化指定异常为来自或者包含指定异常- Type Parameters:
T
- 异常类型- Parameters:
throwable
- 异常exceptionClass
- 定义的引起异常的类- Returns:
- 结果为null 不是来自或者包含
- Since:
- 4.3.2
-
convertFromOrSuppressedThrowable
public static <T extends Throwable> T convertFromOrSuppressedThrowable(Throwable throwable, Class<T> exceptionClass, boolean checkCause) 转化指定异常为来自或者包含指定异常- Type Parameters:
T
- 异常类型- Parameters:
throwable
- 异常exceptionClass
- 定义的引起异常的类checkCause
- 判断cause- Returns:
- 结果为null 不是来自或者包含
- Since:
- 4.4.1
-
getThrowableList
- Parameters:
throwable
- 异常对象,可以为null- Returns:
- 异常链中所有异常集合
- Since:
- 4.6.2
-
getRootCause
获取异常链中最尾端的异常,即异常最早发生的异常对象。
此方法通过调用Throwable.getCause()
直到没有cause为止,如果异常本身没有cause,返回异常本身
传入null返回也为null此方法来自Apache-Commons-Lang3
- Parameters:
throwable
- 异常对象,可能为null- Returns:
- 最尾端异常,传入null参数返回也为null
-
getRootCauseMessage
获取异常链中最尾端的异常的消息,消息格式为:{SimpleClassName}: {ThrowableMessage}- Parameters:
th
- 异常- Returns:
- 消息
- Since:
- 4.6.2
-