IDEA Debug 时模拟抛出异常
IDEA Debug 大约 746 字场景
需要测试代码异常情况,运行时模拟抛出异常。
比如模拟OutOfMemoryError
内存溢出OOM
异常等。
需要测试Spring
全局的ControllerAdvice
可以使用这个方法。
测试代码
断点打在System.out.println("aaa");
该行。
public class MockThrowExceptionDemo {
public static void main(String[] args) {
System.out.println(mockThrowException());
}
public static String mockThrowException() {
System.out.println("aaa");
return "ok";
}
}
步骤
Debugger
的Frames
断点停顿处,右键运行的方法,点击Throw Exception
。- 弹出对话框,输入需要模拟抛出的异常,比如
RuntimeException
,再点击OK
。 - 此时程序会跳转到
Thread
类中的dispatchUncaughtException
这个方法,直接点击放行。(示例代码未使用try...catch
捕获异常,所以会跳转到dispatchUncaughtException
方法,反之需要多次点击Throw Exception
,直到进入catch
代码块) - 此时控制台输出异常信息。
示意图
相关视频
阅读 3618 · 发布于 2022-08-07
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓
昵称:
随便看看
换一批
-
软考-系统架构设计师:E-R 模型阅读 2362
-
JVM:运行时数据区之程序计数器阅读 990
-
IDEA 使用 Kubernetes 瑞士军刀 Telepresence 进行网络代理阅读 1231
-
Ubuntu 修改 apt 镜像阅读 4323
-
Rust 标准库 API 输入 stdin 输出 stdout 错误输出 stderr阅读 564
-
Spring、SpringMVC、Spring Boot 中的注解阅读 1316
-
Vue <component> 实现组件动态加载阅读 1741
-
Linux 之 Ubuntu apt-get 更换镜像阅读 3580
-
k3s 部署简化版 Kubernetes阅读 1522
-
Spring Boot 2.3.0 不再包含 Validation阅读 2351