190909-Java之输出异常堆栈信息

文章目录
  1. II. 其他
    1. 1. 一灰灰Blog: https://liuyueyi.github.io/hexblog
    2. 2. 声明
    3. 3. 扫描关注

在代码出现异常时,堆栈信息可以有效的辅助定位和排除问题,异常堆栈一般是怎么打印的呢

下面是一个简单的输出cas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private int divide(int a) {
return a / 0;
}

@Test
public void test() {
try {
divide(10);
} catch (Exception e) {
System.out.println(e);
System.out.println("----------");
e.printStackTrace();
}
}

直接使用 System.out输出时,看不到堆栈信息;使用e.printStackTrace();可以看到堆栈信息,输出如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
java.lang.ArithmeticException: / by zero
----------
java.lang.ArithmeticException: / by zero
at com.git.hui.print.PrintTest.divide(LogUtilTest.java:11)
at com.git.hui.print.PrintTest.test(LogUtilTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

当然在我们实际的项目中,一般既不会使用e.printStackTrace();方式输出异常堆栈,也不会使用System.out方式输出;更常见的是通过专用的日志组件来处理,比如logback,slf4j2等

前不久做支持一个java动态编译运行的项目时,遇到的一个问题是我们通过控制台来控制某块java代码的执行,如果出现异常时,我希望将异常堆栈返回给控制台来查看,所以我希望可以从Throwable中获取堆栈信息,并转成String,那么可以怎么做呢?

主要的实现就是参考e.printStackTrace的实现,将输出到控制台的流,转换为我们自定义的额字符流

1
2
3
4
5
6
7
8
9
10
11
public static String getThrowableStackInfo(Throwable e) {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
e.printStackTrace(new java.io.PrintWriter(buf, true));
String msg = buf.toString();
try {
buf.close();
} catch (Exception t) {
return e.getMessage();
}
return msg;
}

然后再次测试,结果如下

II. 其他

1. 一灰灰Bloghttps://liuyueyi.github.io/hexblog

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

3. 扫描关注

一灰灰blog

QrCode

知识星球

goals

# Java

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×