1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

Similar documents
chp6.ppt


EJB-Programming-4-cn.doc

JavaIO.PDF

untitled

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

《大话设计模式》第一章

Microsoft Word - 01.DOC

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

untitled

Java

Java ¿ª·¢ 2.0: Óà Hadoop MapReduce ½øÐдóÊý¾Ý·ÖÎö

untitled

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

Chapter 9: Objects and Classes

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

電機工程系認可證照清單 /7/1

"+handlermethod.getbean().getclass().getname()); public void aftercompletion(httpservletrequest req, HttpServletResponse resp, Object handler, Excepti

untitled

Microsoft Word - 2AF63內文.doc

使用MapReduce读取XML文件

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

untitled

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

Microsoft Word - 投影片ch13

1.5招募说明书(草案)

java2d-4.PDF

EJB-Programming-3.PDF

FileMaker 15 ODBC 和 JDBC 指南

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Microsoft Word - JAVA Programming Language Homework VI_ans.doc

北 风 网 讲 师 原 创 作 品 ---- 仅 供 学 员 内 部 交 流 使 用 前 言 吾 尝 终 日 而 思 矣, 不 如 须 臾 之 所 学 也 ; 吾 尝 跂 而 望 矣, 不 如 登 高 之 博 见 也 登 高 而 招, 臂 非 加 长 也, 而 见

用手機直接傳值不透過網頁連接, 來當作搖控器控制家電 ( 電視遙控器 ) 按下按鍵發送同時會回傳值來確定是否有送出 問題 :1. 應該是使用了太多 thread 導致在傳值上有問題 2. 一次按很多次按鈕沒辦法即時反應

FileMaker 16 ODBC 和 JDBC 指南

2009年9月全国计算机等级考试二级Java真题及答案

Microsoft PowerPoint - 09Future

Swing-02.pdf

Hive:用Java代码通过JDBC连接Hiveserver

内容介绍 6.1 任务预览 6.2 异常 6.3 异常种类与层次结构 6.4 异常处理代码块 try-catch-finally 6.5 throw 语句与 throws 子句 6.6 自定义异常类 6.7 异常处理代码块嵌套 6.8 错误与断言 6.9 本章小结 6.10 实训 6: 除法运算程序

目 录 实 验 一 : 熟 悉 Java 开 发 环 境... 1 实 验 二 : 类 与 面 向 对 象... 7 实 验 三 : 继 承 和 多 态 实 验 四 : 异 常 处 理 实 验 五 : 流 文 件 及 基 于 文 本 的 应 用... 23

Java 1 Java String Date

拦截器(Interceptor)的学习

目 录 / CATALOG 一 桌 面 网 站 接 入 1. JAVASCRIPT 网 页 插 件 接 入 2. JS 代 码 操 作 指 引 3. 网 页 链 接 独 立 页 面 接 入 4. 网 页 链 接 接 入 操 作 指 引 5. 自 定 义 桌 面 网 站 接 入 图 标 颜 色 等 0

单位代码:10698

投影片 1

Flume-ng与Mysql整合开发

三种方法实现Hadoop(MapReduce)全局排序(1)

2. AOP 底层技术实现 小风 Java 实战系列教程 关键词 : 代理模式 代理模型分为两种 : 1) 接口代理 (JDK 动态代理 ) 2) 子类代理 (Cglib 子类代理 ) 需求 :CustomerService 业务类, 有 save,update 方法, 希望在 save,updat

JBuilder Weblogic

回 页 首 相 关 的 类 和 接 口 要 了 解 Java 动 态 代 理 的 机 制, 首 先 需 要 了 解 以 下 相 关 的 类 或 接 口 : java.lang.reflect.proxy: 这 是 Java 动 态 代 理 机 制 的 主 类, 它 提 供 了 一 组 静 态 方 法

IoC容器和Dependency Injection模式.doc

开放数据处理服务 ODPS 批量数据通道

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

(Microsoft Word - \272\364\263q\245|\244A_ _\304\254\253\330\336\263__\272\353\302\262\263\370\247i.doc)

建模与图形思考

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

不 是 想 了 想 又 说, 身 体 有 点 不 舒 服, 过 来 看 看 哦, 怎 么 了? 许 是 出 于 职 业 习 惯, 谭 清 辰 脱 口 而 出 你 是 外 科 大 夫 吧? 妇 科 的 毛 病 你 也 能 治? 田 惜 菁 明 显 不 愿 意 继 续 这 个 话 题, 说 话 语 气

return gethexstr messagedigest.digest) ) int len = in.length; int i; StringBuilder out = new StringBuilder len * 2 for i = 0; i < len; i++ ) catch Exc

Learning Java

D getinitparameternames() 9 下 列 选 项 中, 属 于 Servlet API 中 提 供 的 request 对 象 的 包 装 类 的 是 ( ) A HttpServletRequestWrapper B HttpServletRequest C HttpServ

BPS6.0 Web服务集成和短流程功能使用手册

OOP with Java 通知 Project 3: 3 月 29 日晚 9 点 4 月 1 日上课

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

( 含 要 ) 1-2 用 或 雇 用, 抑 或 有 無 俸 給 文 職 或 武 職, 政 官 或 事 官 均 屬 之, 其 不 以 具 備 人 資 格 為 限, 因 此 屬 於 最 廣 義 之 念 四 廣 義 念 之 依 服 24 條 之 規 定 : 本 於 受 有 俸 給 之 文 武 職, 及

Chapter 1 選 用 好 的 燜 燒 罐 選 用 好 的 燜 燒 罐 是 做 好 燜 燒 罐 料 理 最 重 要 的 步 驟, 除 了 須 注 意 使 用 的 材 質 是 否 符 合 食 器 使 用 標 準, 也 須 注 意 燜 燒 罐 的 保 溫 效 果, 才 能 安 心 享 用 燜 燒 罐

Microsoft Word - ch04三校.doc

4.1 AMI MQSeries API AMI MQI AMI / / AMI JavaC C++ AMI / AMI AMI - / /

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

Chapter 9: Objects and Classes

输入 project name 选择完成

while ((ch = fr.read())!= -1) { System.out.print((char) ch); fr.close(); 例 3: 用 BufferedReader 读 TXT 文件 public class FileReaderDemo3 { public static v

中 文 摘 要 智 慧 型 手 機 由 於 有 強 大 的 功 能, 以 及 優 渥 的 便 利 性, 還 能 與 網 路 保 持 隨 時 的 鏈 結 與 同 步 更 新, 因 此 深 受 廣 大 消 費 者 喜 愛, 當 然, 手 機 遊 戲 也 成 為 現 代 人 不 可 或 缺 的 娛 樂 之

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

Java程序设计

untitled

Microsoft Word - Broker.doc

OOP with Java 通知 Project 4: 5 月 2 日晚 9 点

javac: 负 责 的 是 编 译 的 部 分, 当 执 行 javac 时, 会 启 动 java 的 编 译 器 程 序 对 指 定 扩 展 名 的.java 文 件 进 行 编 译 生 成 了 jvm 可 以 识 别 的 字 节 码 文 件 也 就 是 class 文 件, 也 就 是 ja

OOP with Java 通知 Project 4: 4 月 19 日晚 9 点

javaexample-02.pdf

RUN_PC連載_12_.doc



Microsoft Word doc

关于天云趋势 天云趋势由宽带资本和趋势科技共同投资成立于 2010 年 3 月 趋势科技是 Hadoop 的重度使用者 : 2006 年开始使用, 用于处理网页和邮件服务器评级 五个数据中心, 近 1000 个节点, 最大集群约 500 台服务器 日均处理 3.6T 日志数据 亚洲最早, 也是最大的

全国计算机技术与软件专业技术资格(水平)考试

Android Service

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

Microsoft PowerPoint - UTMock_30.pptx

四、實務實習課程之實習工作日誌(請貼上掃描檔)

3.1 num = 3 ch = 'C' 2

1.5招募说明书(草案)

新版 明解C++入門編

<4D F736F F D20C8EDC9E82DCFC2CEE7CCE22D3039C9CF>

Stateless Session Beans(无状态bean)的学习

Transcription:

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