Class ExceptionUtil

java.lang.Object
com.sure.tool.util.ExceptionUtil

public class ExceptionUtil extends Object
异常工具类:堆栈转字符串、根源异常定位等,零依赖。
Since:
0.2.0
Author:
suretool
  • Method Details

    • getStackTrace

      public static String getStackTrace(Throwable throwable)
      将异常堆栈转为多行字符串。
      Parameters:
      throwable - 异常
      Returns:
      堆栈字符串
    • getRootCause

      public static Throwable getRootCause(Throwable throwable)
      定位最根本的异常(cause 链末端)。
      Parameters:
      throwable - 异常
      Returns:
      根源异常(无 cause 时返回自身)
    • getMessage

      public static String getMessage(Throwable throwable)
      获取异常的简要信息(类型 + 消息)。
      Parameters:
      throwable - 异常
      Returns:
      如 java.io.IOException: 文件不存在
    • isCausedBy

      public static boolean isCausedBy(Throwable throwable, Class<? extends Throwable> type)
      是否由指定类型异常导致(沿 cause 链查找)。
      Parameters:
      throwable - 异常
      type - 目标类型
      Returns:
      是否匹配
    • wrap

      public static RuntimeException wrap(Throwable throwable)
      将受检异常包装为运行时异常(已是运行时异常则直接返回)。
      Parameters:
      throwable - 异常
      Returns:
      RuntimeException
    • unwrap

      public static Throwable unwrap(Throwable throwable)
      解包装异常:逐层剥离包装异常,返回最内层异常。
      Parameters:
      throwable - 异常
      Returns:
      最内层异常;null 返回 null