全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-08 6 分钟 ✍️ juanwangdev

ExceptionUtils

ExceptionUtils是Apache Commons Lang提供的异常处理工具类。

概述

所在包

org.apache.commons.lang3.exception.ExceptionUtils

依赖引入

XML
<dependency>
    <groupId>org.apache.commons.lang3</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

核心特点

  • 静态方法,无需创建对象
  • 空安全,null异常不会抛NPE
  • 提取异常信息、堆栈、根异常等

获取异常信息

getMessage获取消息

Java
try {
    // ...
} catch (Exception e) {
    String msg = ExceptionUtils.getMessage(e);
    // 返回异常消息(如 "Division by zero")
    
    // 等价于 e.getMessage(),但空安全
    String msg = ExceptionUtils.getMessage(null);  // ""
}

getRootCauseMessage获取根异常消息

Java
try {
    // ...
} catch (Exception e) {
    String msg = ExceptionUtils.getRootCauseMessage(e);
    // 返回异常链最底层异常的消息
}

获取堆栈

getStackTrace堆栈字符串

Java
try {
    // ...
} catch (Exception e) {
    String stackTrace = ExceptionUtils.getStackTrace(e);
    // 返回完整堆栈跟踪字符串
    
    System.out.println(stackTrace);
    // 输出:
    // java.lang.NullPointerException: message
    //     at com.example.Test.method(Test.java:10)
    //     at ...
}

// 便于日志记录
logger.error("异常: " + ExceptionUtils.getStackTrace(e));

getStackFrames堆栈数组

Java
String[] frames = ExceptionUtils.getStackFrames(e);
// 返回堆栈每一行的数组

for (String frame : frames) {
    System.out.println(frame);
}

异常链处理

getRootCause获取根异常

Java
try {
    // ...
} catch (Exception e) {
    Throwable root = ExceptionUtils.getRootCause(e);
    // 返回异常链中最初导致问题的异常
    
    // 如果e是包装异常,root是原始异常
    // 如果没有异常链,root是e本身
}

getThrowableList获取异常链列表

Java
try {
    // ...
} catch (Exception e) {
    List<Throwable> list = ExceptionUtils.getThrowableList(e);
    // 返回异常链中所有异常的List
    
    // list[0]是顶层异常,list[n]是根异常
}

indexOfThrowable查找特定异常

Java
try {
    // ...
} catch (Exception e) {
    // 查找异常链中IOException的位置
    int index = ExceptionUtils.indexOfThrowable(e, IOException.class);
    // 返回索引,-1表示不存在
}

indexOfType查找异常类型

Java
int index = ExceptionUtils.indexOfType(e, IOException.class);
// 查找异常链中指定类型异常的位置(包括子类)

异常计数

countThrowables统计异常链长度

Java
int count = ExceptionUtils.countThrowables(e);
// 返回异常链中的异常数量

// 如:BusinessException → IOException → FileNotFoundException
// count = 3

异常创建

rethrow重新抛出

Java
try {
    // ...
} catch (Exception e) {
    throw ExceptionUtils.rethrow(e);  // 重新抛出异常
}

wrap包装异常

Java
try {
    // ...
} catch (IOException e) {
    throw ExceptionUtils.wrapThrowables(e, BusinessException.class);
    // 包装IOException为BusinessException
}

使用场景

日志记录

Java
try {
    // 业务代码
} catch (Exception e) {
    // 记录完整堆栈
    logger.error("业务异常: {}", ExceptionUtils.getStackTrace(e));
    
    // 或只记录根异常
    Throwable root = ExceptionUtils.getRootCause(e);
    logger.error("根异常: {}", root.getMessage());
}

异常分析

Java
try {
    // ...
} catch (Exception e) {
    // 检查异常链中是否有特定类型
    if (ExceptionUtils.indexOfType(e, IOException.class) >= 0) {
        // 处理IO相关异常
    }
}

与原生方法对比

功能原生方法ExceptionUtils
获取消息e.getMessage()(可能null)getMessage(e)(空安全)
获取堆栈e.printStackTrace()(打印)getStackTrace(e)(返回String)
获取根异常循环getCause()getRootCause(e)
堆栈数组getStackFrames(e)

要点总结

  • ExceptionUtils是Apache Commons Lang的异常工具类
  • getMessage获取异常消息(空安全)
  • getStackTrace获取完整堆栈字符串
  • getRootCause获取异常链根异常
  • getThrowableList获取异常链列表
  • indexOfThrowable查找特定异常位置
  • countThrowables统计异常链长度
  • 便于日志记录和异常分析

📝 发现内容有误?点击此处直接编辑

← 上一篇 DateUtils
下一篇 → FileUtils
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库