public class Throwables extends Object
| 限定符和类型 | 方法和说明 |
|---|---|
static Throwable |
getRootCause(Throwable ex)
根据异常栈获取到 root cause
|
static String |
getStackTraceAsString(Throwable throwable)
已过时。
|
static <I,O> O |
ignoreExceptions(org.slf4j.Logger logger,
O valueIfError,
List<Class<Throwable>> throwables,
ThrowableFunction<I,O> func,
I arg)
执行指定的function,如果执行过程中,出现了指定的那些异常 throwables,则返回 valuesIfError,其他的异常则抛出。
|
static <I1,I2,O> O |
ignoreExceptions(org.slf4j.Logger logger,
O valueIfError,
List<Class<Throwable>> throwables,
ThrowableFunction2<I1,I2,O> func,
I1 arg1,
I2 arg2)
执行指定的function,如果执行过程中,出现了指定的那些异常 throwables,则返回 valuesIfError,其他的异常则抛出。
|
static <I,O> O |
ignoreThrowable(org.slf4j.Logger logger,
O valueIfError,
ThrowableFunction<I,O> func,
I arg)
当执行某个Function时,出现任何Exception,Error时,则返回默认值。
|
static <I1,I2,O> O |
ignoreThrowable(org.slf4j.Logger logger,
O valueIfError,
ThrowableFunction2<I1,I2,O> func,
I1 arg1,
I2 arg2)
当执行某个Function时,出现任何Exception,Error时,则返回默认值。
|
static List<StackTraceElement> |
lazyStackTrace(Throwable throwable)
Returns the stack trace of
throwable, possibly providing slower iteration over the full
trace but faster iteration over parts of the trace. |
static boolean |
lazyStackTraceIsLazy()
Returns whether
lazyStackTrace(java.lang.Throwable) will use the special implementation described in its
documentation. |
static void |
log(org.slf4j.Logger logger,
Level level,
String message,
Throwable ex)
把异常记录到日志中
|
static void |
log(Throwable ex) |
static String |
stringify(Throwable t)
* 把异常栈dump到一个字符串中
|
static void |
throwAsRuntimeException(Throwable ex)
把任何异常转为 RuntimeException,并throw
|
static Throwable |
throwIfError(Throwable ex)
当 ex instanceof Error 时,抛出该Error
|
static Throwable |
throwIfIOException(Throwable ex)
当 ex instanceof IOException 时,抛出该 Exception
|
static Throwable |
throwIfRuntimeException(Throwable ex)
当 ex instanceof RuntimeException 时,抛出该 Exception
|
static Throwable |
throwRootCauseIfIOException(Throwable ex)
step1 : get root cause
step2 : throwIfIOException(root cause)
|
static RuntimeException |
wrapAsRuntimeException(Throwable ex)
当 !
|
static RuntimeIOException |
wrapAsRuntimeIOException(IOException ex) |
public static String stringify(Throwable t)
t - 和getStackTraceAsString同一个作用public static String getStackTraceAsString(Throwable throwable)
stringify(Throwable)public static Throwable throwIfRuntimeException(Throwable ex)
public static Throwable throwIfIOException(Throwable ex) throws IOException
IOExceptionpublic static RuntimeException wrapAsRuntimeException(Throwable ex)
public static void throwAsRuntimeException(Throwable ex)
public static RuntimeIOException wrapAsRuntimeIOException(IOException ex)
public static Throwable throwRootCauseIfIOException(Throwable ex) throws IOException
IOExceptionpublic static void log(@Nullable org.slf4j.Logger logger, @Nullable Level level, @Nullable String message, @NonNull Throwable ex)
logger - the logger,默认为 Throwables.loggerlevel - log level, 默认为 ERRORmessage - 异常消息ex - 异常public static List<StackTraceElement> lazyStackTrace(Throwable throwable)
throwable, possibly providing slower iteration over the full
trace but faster iteration over parts of the trace. Here, "slower" and "faster" are defined in
comparison to the normal way to access the stack trace, throwable.getStackTrace(). Note, however, that this method's special implementation is not
available for all platforms and configurations. If that implementation is unavailable, this
method falls back to getStackTrace. Callers that require the special implementation can
check its availability with lazyStackTraceIsLazy().
The expected (but not guaranteed) performance of the special implementation differs from
getStackTrace in one main way: The lazyStackTrace call itself returns quickly
by delaying the per-stack-frame work until each element is accessed. Roughly speaking:
getStackTrace takes stackSize time to return but then negligible time to
retrieve each element of the returned list.
lazyStackTrace takes negligible time to return but then 1/stackSize time
to retrieve each element of the returned list (probably slightly more than 1/stackSize).
Note: The special implementation does not respect calls to throwable.setStackTrace. Instead, it always reflects the original stack trace from the
exception's creation.
public static boolean lazyStackTraceIsLazy()
lazyStackTrace(java.lang.Throwable) will use the special implementation described in its
documentation.public static <I,O> O ignoreThrowable(@Nullable org.slf4j.Logger logger, @Nullable O valueIfError, @NonNull ThrowableFunction<I,O> func, @Nullable I arg)
I - Function的输入参数类型O - Function的输出参数类型logger - 当出现throwable时,用该 logger 去做日志记录valueIfError - 当出现 throwable时,返回该值func - 要执行的functionarg - function 执行时,需要传递的参数public static <I1,I2,O> O ignoreThrowable(@Nullable org.slf4j.Logger logger, @Nullable O valueIfError, @NonNull ThrowableFunction2<I1,I2,O> func, @Nullable I1 arg1, @Nullable I2 arg2)
I1 - FunctionI2 - FunctionO - Functionlogger - 当出现throwable时,用该 logger 去做日志记录valueIfError - 当出现 throwable时,返回该值func - 要执行的functionarg1 - function 执行时,需要传递的参数arg2 - function 执行时,需要传递的参数public static <I,O> O ignoreExceptions(@Nullable org.slf4j.Logger logger, @Nullable O valueIfError, @NonNull List<Class<Throwable>> throwables, @NonNull ThrowableFunction<I,O> func, @Nullable I arg)
I - func的输入参数类型O - func的输出参数类型logger - 当出现throwable时,用该 logger 去做日志记录valueIfError - 当出现 指定的异常时,返回该值throwables - 指定的异常func - 要执行的函数arg - func的参数public static <I1,I2,O> O ignoreExceptions(@Nullable org.slf4j.Logger logger, @Nullable O valueIfError, @NonNull List<Class<Throwable>> throwables, @NonNull ThrowableFunction2<I1,I2,O> func, @Nullable I1 arg1, @Nullable I2 arg2)
I1 - func的第一个输入参数类型I2 - func的第二个输入参数类型O - func的输出参数类型logger - 当出现throwable时,用该 logger 去做日志记录valueIfError - 当出现 指定的异常时,返回该值throwables - 指定的异常func - 要执行的函数arg1 - func的第一个参数arg2 - func的第二个参数Copyright © 2021. All rights reserved.