Chapter 15. Suppressed Exception CH14 Finally Block Java SE 7 try-with-resources JVM cleanup try-with-resources JVM cleanup cleanup Java SE 7 Throwable getsuppressed Throwable[] getsuppressed() Suppressed getsuppressed cleanup suppressed exception cleanup 15-1 MyOutputStream JVM MyOutputStream AutoCloseable close IOException 121
1: public class MyOutputStream implements AutoCloseable { 2: @Override 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6: } 15-1 AutoCloseable close try-with-resources MyOutputStream 15-2 9 例外處理設計的逆襲122 1: package tw.teddysoft.exception.ehm; 2: import java.io.ioexception; 3: import java.sql.sqlexception; 4: 5: public class Java7TryWithOneResource { 6: public static void Java7TryWithResource() 7: throws IOException, SQLException{ 8: 9: try( MyOutputStream mos = new MyOutputStream()){ 10: throw new IOException("Function failure"); 11: } 12: } 13: 14: public static void main (String[] args) { 15: try { 16: Java7TryWithResource(); 17: } catch (Exception e) { 18: e.printstacktrace(); 19: Throwable [] ta = e.getsuppressed(); 20: System.err.println("Suppressed exception " 21: + "size = " + ta.length); 22: } 23: } 24: } 15-2 try-with-resources MyOutputStream
main Java7TryWithResource 10 IOException Java7TryWithResource IOException Java7TryWithResource MyOutputStream close java.io.ioexception: Function failure at tw.teddysoft.exception.ehm.java7trywithoneresource.java7t rywithresource(java7trywithoneresource.java:10) at tw.teddysoft.exception.ehm.java7trywithoneresource. main(java7trywithoneresource.java:16) Suppressed: java.io.ioexception at tw.teddysoft.exception.ehm.myoutputstream. close(myoutputstream.java:8) at tw.teddysoft.exception.ehm.java7trywithoneresource.jav a7trywithresource(java7trywithoneresource.java:11)... 1 more Suppressed exception size = 1 JVM cleanup cleanup main Java7TryWithResource IOException function failure MyOutputStream close IOException cleanup failure function failure IOException suppressed exception 15. Suppressed Exception 搶救例外大作戰 123
橋歸橋, 路歸路 ( 理論上啦 ) try-with-resources function failure function failure suppressed exception cleanup 1 function failure suppressed exception getsuppressed suppressed exception 例外處理設計的逆襲124 try block catch block JVM try-with-resources cleanup cleanup cleanup 15-3 MyOutputStream MyConnection MyInputStream close IOException SQLException FileNotFoundException 1: package tw.teddysoft.exception.ehm; 2: import java.io.ioexception; 3: import java.sql.sqlexception; 4: 5: public class Java7TryWithMultipleResources { 6: public static void Java7TryWithResources() 7: throws IOException, SQLException{ 8: 9: try( MyOutputStream mos = new MyOutputStream(); 10: MyConnection mc = new MyConnection(); 11: MyInputStream mis = new MyInputStream()){ 12: } 13: } 14: 1 Function failure exception 夾帶著 suppressed exception 並不一定都表示 cleanup 失敗, 也有可能是 catch block 執行錯誤狀態回復動作失敗, 在本章只討論 cleanup 失敗的狀況
15: public static void main (String[] args) { 16: try { 17: Java7TryWithResources(); 18: } catch (Exception e) { 19: e.printstacktrace(); 20: Throwable [] ta = e.getsuppressed(); 21: System.err.println("Suppressed exception " 22: + "size = " + ta.length); 23: } 24: } 25: } 15-3 try-with-resources close main Java7TryWithResources FileNotFoundException suppressed exception SQLException IOException JVM cleanup close java.io.filenotfoundexception at tw.teddysoft.exception.ehm.myinputstream. close(myinputstream.java:9) at tw.teddysoft.exception.ehm.java7trywithmultipleresources.ja va7trywithresources(java7trywithmultipleresources.java:12) at tw.teddysoft.exception.ehm.java7trywithmultipleresources. main(java7trywithmultipleresources.java:17) Suppressed: java.sql.sqlexception at tw.teddysoft.exception.ehm.myconnection. close(myconnection.java:9) Suppressed: java.io.ioexception at tw.teddysoft.exception.ehm.myoutputstream. close(myoutputstream.java:8) Suppressed exception size = 2 15. Suppressed Exception 搶救例外大作戰 125
*** suppressed exception try block catch block cleanup 15-3 suppressed exception try block catch block function failure cleanup cleanup failure 例外處理設計的逆襲126 CH17 Suppressed Exception *** 仔細讀, 會讀出一點心得
Chapter 16. Cleanup Failure CH15 Suppressed Exception cleanup failure Java SE 7 try-with-resources Teddy 127
定義 Cleanup Failure 語意 Java cleanup failure CleanupException cleanup failure 16-1 例外處理設計的逆襲128 1: package tw.teddysoft.exception.ehm.cleanup; 2: 3: public class CleanupException extends RuntimeException { 4: public CleanupException(Exception e) { 5: super(e); 6: } 7: } 16-1 CleanupException CleanupException RuntimeException unchecked exception checked exception CleanupException Teddy function failure CleanupException checked exception Ignored Checked Exception CH34 cleanup design fault component fault cleanup Teddy Java close
CleanupFailure cleanup failure CleanupException runtime exception design fault CleanupException CleanupException 修改 close 函數所丟出的例外 CleanupException MyConnection MyInputStream MyOutputStream close SQLException FileNotFoundException IOException CleanupException 16. 1: package tw.teddysoft.exception.ehm.cleanup; 2: import java.sql.sqlexception; 3: 4: public class MyConnection implements AutoCloseable { 5: 6: @Override 7: public void close() throws CleanupException { 8: try{ 9: throw new SQLException(); 10: } catch(exception e){ 129
11: throw new CleanupException(e); 12: } 13: } 14: } 16-2 MyConnection CleanupException 例外處理設計的逆襲130 1: package tw.teddysoft.exception.ehm.cleanup; 2: import java.io.filenotfoundexception; 3: 4: public class MyInputStream implements AutoCloseable { 5: @Override 6: public void close() throws CleanupException { 7: try{ 8: throw new FileNotFoundException(); 9: } catch(exception e){ 10: throw new CleanupException(e); 11: } 12: } 13: } 16-3 MyInputStream CleanupException 1: package tw.teddysoft.exception.ehm.cleanup; 2: import java.io.ioexception; 3: 4: public class MyOutputStream implements AutoCloseable { 5: @Override 6: public void close() throws CleanupException { 7: try{ 8: throw new IOException(); 9: } catch(exception e){ 10: throw new CleanupException(e); 11: } 12: } 13: } 16-4 MyOutputStream CleanupException
CleanupFailure 16-2 16-3 16-4 close CleanupException SQLException FileNotFoundException IOException CleanupException CleanupException CleanupException Cleanup Failure 的語意清楚了嗎? CleanupException cleanup failure 16-5 try-with-resources MyOutputStream MyConnection MyInputStream try block IOException 1: package tw.teddysoft.exception.ehm.cleanup; 2: import java.io.ioexception; 3: 4: public class MultipleCleanupFailuresExample { 5: 6: public static void cleanupfailureintrywithresources() 7: throws IOException{ 8: 9: try( MyOutputStream mos = new MyOutputStream(); 10: MyConnection mc = new MyConnection(); 11: MyInputStream mis = new MyInputStream()){ 12: 13: throw new IOException("Function Failure"); 14: } 15: } 16: 17: public static void main (String[] args) { 18: try { 16. 131
19: cleanupfailureintrywithresources(); 20: } catch (Exception e) { 21: e.printstacktrace(); 22: Throwable [] ta = e.getsuppressed(); 23: System.err.println("Suppressed exception " 24: + "size = " + ta.length); 25: } 26: } 27: } 16-5 try block cleanup failure main cleanupfailureintrywithresources 例外處理設計的逆襲132 try block failure exception suppressed exception CleanupException CleanupException FileNotFoundException SQLException IOException java.io.ioexception: Function Failure at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample.cleanupFailureInTryWithResou rces(multiplecleanupfailuresexample.java:13) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample.main(MultipleCleanupFailures Example.java:19) Suppressed: tw.teddysoft.exception.ehm.cleanup. CleanupException: java.io.filenotfoundexception at tw.teddysoft.exception.ehm.cleanup.myinputstream. close(myinputstream.java:10) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample.cleanupFailureInTryWithResou rces(multiplecleanupfailuresexample.java:14)... 1 more Caused by: java.io.filenotfoundexception at tw.teddysoft.exception.ehm.cleanup.myinputstream. close(myinputstream.java:8)
CleanupFailure Suppressed: tw.teddysoft.exception.ehm.cleanup. CleanupException: java.sql.sqlexception at tw.teddysoft.exception.ehm.cleanup.myconnection. close(myconnection.java:11) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample.cleanupFailureInTryWithResou rces(multiplecleanupfailuresexample.java:14)... 1 more Caused by: java.sql.sqlexception at tw.teddysoft.exception.ehm.cleanup.myconnection. close(myconnection.java:9) Suppressed: tw.teddysoft.exception.ehm.cleanup. CleanupException: java.io.ioexception at tw.teddysoft.exception.ehm.cleanup.myoutputstream. close(myoutputstream.java:10) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample.cleanupFailureInTryWithResou rces(multiplecleanupfailuresexample.java:14)... 1 more Caused by: java.io.ioexception at tw.teddysoft.exception.ehm.cleanup.myoutputstream. close(myoutputstream.java:8) Suppressed exception size = 3 *** 16. cleanup failure try block JVM close 16-6 133
例外處理設計的逆襲134 1: package tw.teddysoft.exception.ehm.cleanup; 2: import java.io.ioexception; 3: 4: public class MultipleCleanupFailuresExample2 { 5: 6: public static void cleanupfailureintrywithresources() 7: throws IOException{ 8: 9: try( MyOutputStream mos = new MyOutputStream(); 10: MyConnection mc = new MyConnection(); 11: MyInputStream mis = new MyInputStream()){ 12: } 13: } 14: 15: public static void main (String[] args) { 16: try { 17: cleanupfailureintrywithresources(); 18: } catch (Exception e) { 19: e.printstacktrace(); 20: Throwable [] ta = e.getsuppressed(); 21: System.err.println("Suppressed exception " 22: + "size = " + ta.length); 23: } 24: } 25: } 16-6 close cleanup failure main cleanupfailureintrywithresources CleanupException suppressed exception CleanupException CleanupException FileNotFoundException SQLException IOException tw.teddysoft.exception.ehm.cleanup.cleanupexception: java. io.filenotfoundexception at tw.teddysoft.exception.ehm.cleanup.myinputstream.
CleanupFailureclose(MyInputStream.java:10) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample2.cleanupFailureInTryWithReso urces(multiplecleanupfailuresexample2.java:12) at tw.teddysoft.exception.ehm.cleanup. MultipleCleanupFailuresExample2.main(MultipleCleanupFailure sexample2.java:17) Suppressed: tw.teddysoft.exception.ehm.cleanup. CleanupException: java.sql.sqlexception at tw.teddysoft.exception.ehm.cleanup.myconnection. close(myconnection.java:11) Caused by: java.sql.sqlexception at tw.teddysoft.exception.ehm.cleanup.myconnection. close(myconnection.java:9) Suppressed: tw.teddysoft.exception.ehm.cleanup. CleanupException: java.io.ioexception at tw.teddysoft.exception.ehm.cleanup.myoutputstream. close(myoutputstream.java:10) Caused by: java.io.ioexception at tw.teddysoft.exception.ehm.cleanup.myoutputstream. close(myoutputstream.java:8) Caused by: java.io.filenotfoundexception at tw.teddysoft.exception.ehm.cleanup.myinputstream. close(myinputstream.java:8) Suppressed exception size = 2 16. *** function failure cleanup failure 135
Java AutoCloseable close CleanupException Teddy Java *** 這裡面還是有點學問滴 例外處理設計的逆襲136