在查看 JAVA 应用抛出的异常堆栈以排查问题时,我们有时会看到所谓 suppressed exceptions,即被抑制的异常。理解 suppressed exceptions 的原理,对我们分析问题的底层真实原因大有裨益。所以本文分析总结下 Java 中的 suppressed exceptions。
java.lang.NullPointerExceptionat com.keep.bdata.SuppressedExceptionsDemo.demoExceptionWithNoSuppress(SuppressedExceptionsDemo.java:21)at com.keep.bdata.SuppressedExceptionsDemo.givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException(SuppressedExceptionsDemo.java:12)
图片
java.lang.NullPointerException at com.keep.bdata.SuppressedExceptionsDemo.demoExceptionWithSuppressed(SuppressedExceptionsDemo.java:38) at com.keep.bdata.SuppressedExceptionsDemo.givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException_withSuppressed(SuppressedExceptionsDemo.java:27) Suppressed: java.io.FileNotFoundException: /non-existent-path/non-existent-file.txt (系统找不到指定的路径。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileInputStream.<init>(FileInputStream.java:93) at com.keep.bdata.SuppressedExceptionsDemo.demoExceptionWithSuppressed(SuppressedExceptionsDemo.java:33)
图片
java.lang.IllegalArgumentException: Thrown from processSomething() at com.keep.bdata.TryWithResourceDemo$ExceptionalResource.processSomething(TryWithResourceDemo.java:23) at com.keep.bdata.TryWithResourceDemo.demoExceptionalResource(TryWithResourceDemo.java:17) at com.keep.bdata.TryWithResourceDemo.givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException_suppressed(TryWithResourceDemo.java:12) Suppressed: java.lang.NullPointerException: Thrown from close() at com.keep.bdata.TryWithResourceDemo$ExceptionalResource.close(TryWithResourceDemo.java:28) at com.keep.bdata.TryWithResourceDemo.demoExceptionalResource(TryWithResourceDemo.java:18)
图片
package com.keep.bdata;import org.junit.jupiter.api.Test;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;publicclass SuppressedExceptionsDemo { @Test public void givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException() throws IOException { demoExceptionWithNoSuppress("/non-existent-path/non-existent-file.txt"); } public static void demoExceptionWithNoSuppress(String filePath) throws IOException { FileInputStream fileIn = null; try { fileIn = new FileInputStream(filePath); } catch (FileNotFoundException e) { thrownew IOException(e); } finally { fileIn.close(); } } @Test public void givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException_withSuppressed() throws IOException{ demoExceptionWithSuppressed("/non-existent-path/non-existent-file.txt"); } public static void demoExceptionWithSuppressed(String filePath) throws IOException { Throwable firstException = null; FileInputStream fileIn = null; try { fileIn = new FileInputStream(filePath); } catch (IOException e) { firstException = e; } finally { try { fileIn.close(); } catch (NullPointerException npe) { if (firstException != null) { npe.addSuppressed(firstException); } throw npe; } } }}
package com.keep.bdata;import org.junit.jupiter.api.Test;publicclass TryWithResourceDemo { @Test public void givenNonExistentFileName_whenAttemptFileOpen_thenNullPointerException_suppressed() throws Exception { demoExceptionalResource(); } public void demoExceptionalResource() throws Exception { try (ExceptionalResource exceptionalResource = new ExceptionalResource()) { exceptionalResource.processSomething(); } } class ExceptionalResource implements AutoCloseable { public void processSomething() { thrownew IllegalArgumentException("Thrown from processSomething()"); } @Override public void close() throws Exception { thrownew NullPointerException("Thrown from close()"); } }
本文链接:http://www.28at.com/showinfo-26-88927-0.html一篇文章彻底理解 Java 的 Suppressed exceptions 机制
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: 一文彻底搞明白享元模式