在查看 JAVA 應(yīng)用拋出的異常堆棧以排查問(wèn)題時(shí),我們有時(shí)會(huì)看到所謂 suppressed exceptions,即被抑制的異常。理解 suppressed exceptions 的原理,對(duì)我們分析問(wèn)題的底層真實(shí)原因大有裨益。所以本文分析總結(jié)下 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 (系統(tǒng)找不到指定的路徑。) 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.www897cc.com/showinfo-26-88927-0.html一篇文章徹底理解 Java 的 Suppressed exceptions 機(jī)制
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: 一文徹底搞明白享元模式
下一篇: 在.Net開發(fā)中使用Math.NET Filtering開源庫(kù)實(shí)現(xiàn)巴特沃斯濾波器