复旦大学:《面向对象分析和设计》课程资料_Exception handling refactorings

Exception/handling refactorings OOA/OOD xuyingxiao@126.com Fudan University
Exception handling refactorings OOA/OOD xuyingxiao@126.com Fudan University

e 1) Checked exception IOEXception Handle or declare 这类异常都是 EXception的子类 e 2)Unchecked exception ● ArithmeticEXception 这类异常都是 RuntimeEXception的子类,虽然 RuntimeEXception同样也是 EXception的子类, 但是它们是特殊的,它们不能通过 client code来 试图解决,所以称为 Unchecked exception
1) Checked exception: IOException Handle or declare 这类异常都是Exception的子类 。 2) Unchecked exception: ArithmeticException 这类异常都是RuntimeException的子类,虽然 RuntimeException同样也是Exception的子类, 但是它们是特殊的,它们不能通过client code来 试图解决,所以称为Unchecked exception

Problem e public void write File(String fileName, String data t Writer writer null may throw an EXCeption * writer= new FileWriter(fileName) / may throw an IOEXception * writer. write(data)
Problem public void writeFile(String fileName, String data){ Writer writer = null; /* may throw an IOException */ writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); }

●Dec|are public void write File(String fileName, String data) throws IoExceptiont Writer writer= nul: /*may throw an IOEXception * writer= new FileWriter(fileName) / may throw an loException * writer. write(data);
Declare public void writeFile(String fileName, String data) throws IOException{ Writer writer = null; /* may throw an IOException */ writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); }

● Handle e public void write File(string fileName, String data Writer writer= null may throw an IOEXception * writer= new FileWriter(fileName) ●●●● may throw an exCeption * writer. write(data) catch(IOEXception e)( / a lot of code*/ finally / code for cleanup
Handle public void writeFile(String fileName, String data){ Writer writer = null; try { /* may throw an IOException */ writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); } catch (IOException e) { /* a lot of code*/ 。。。。。。 } finally {/* code for cleanup */} }

Bad smell ●Hand| e-Bad smell Ignored checked exception public void writeFile(string fileName, String datat Writer writer= null try i / may throw an ioException * writer= new FileWriter(fileName) may throw an IOEXception * writer. write(data) catch(IOEXception e)r/TO Do*/ finally * code for cleanup*/
Bad Smell Handle——Bad Smell: Ignored checked exception public void writeFile(String fileName, String data){ Writer writer = null; try { /* may throw an IOException */ writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); } catch (IOException e) ){ /* TO DO */ } finally {/* code for cleanup */} }

● BAD SMELL a checked exception is caught but nothing is done to deal with it the program is pretending that all is fine when in fact something is wrong
BAD SMELL a checked exception is caught but nothing is done to deal with it, the program is pretending that all is fine when in fact something is wrong

Bad smell ●Hand| e-Bad smell ● Dummy handler e public void write File(String fileName, String data) Writer writer null ●●●● try t /may throw an IOEXception * writer new FileWriter(fileName) / may throw an IOEXception * writer.write(data); catch(IOEXception er e print StackTraceO /or System. out. printIn(e to String) finally/* code for cleanup *1
Bad Smell Handle——Bad Smell: Dummy handler public void writeFile(String fileName, String data){ Writer writer = null; try { /* may throw an IOException */ writer = new FileWriter(fileName); /* may throw an IOException */ writer.write(data); } catch (IOException e){ e.printStackTrace(); // or System.out.println(e.toString()); } finally {/* code for cleanup */} }

21 ackage) 230 public void writeFile(String fileName String data)( Library [eclipse] Writer writer noll may throw an IOException * 26 writer=new FileWriter(fileName): g Add throws declaration /* may throw an IOException"/ Pg surround with try/ catch writer= new File Writer(file Name); 3 catch(IoException e)( l/TODO Auto-generated catch block eprintStackTrace 0

e Solution catch(IOEXception e)) throw e:?
Solution catch (IOException e) ){ throw e; } ?
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 复旦大学:《面向对象分析和设计》课程资料_Error Handling.pdf
- 复旦大学:《面向对象分析和设计》课程资料_AntiPattern_2.Servlet.pdf
- 复旦大学:《面向对象分析和设计》课程资料_AntiPattern_1.JSP J2EE Refactoring Patterns/AntiPatterns.pdf
- 复旦大学:《面向对象分析和设计》课程资料_分析模式_责任模式 Accountability.pdf
- 复旦大学:《面向对象分析和设计》课程资料_分析模式_观察和测量模式.pptx
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_UMLProfile.pdf
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_5.5 Iteration 3 持久化框架 DESIGNING A PERSISTENCE FRAMEWORK WITH PATTERNS.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_5.4 Iteration 3 状态图 MODELING BEHAVIOR IN STATECHART DIAGRAMS.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_5.3 Iteration 3 SSD和合约 ADDING NEW SSDs AND CONTRACTS.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_5.2 Iteration 3 领域模型 MODELING GENERALIZATION & REFINING THE DOMAIN MODEL.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_5.1 Iteration 3 用例加关系.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_4.2 Iteration 2 用GoF设计用例实现 DESIGNING USE-CASE REALIZATIONS WITH GoF DESIGN PATTERNS.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_4.1 Iteration 2 GRASP:MORE PATTERNS.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.5 Iteration 1 实现模型.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.4 Iteration 1 设计模型 GRASP——设计带职责的对象.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.4 Head 2 On to Object Design.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.4 Head 1 Logical Architecture and UML Package Diagrams.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.3 Iteration 1 用例模型——操作合约添加细节.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.2 Iteration 1 用例模型——SSD.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML&GRASP_3.1 Iteration 1 领域模型——概念的可视化.ppt
- 复旦大学:《面向对象分析和设计》课程资料_课程注册系统_1-5 最佳实践、需求、分析设计、架构分析、用例分析.pdf
- 复旦大学:《面向对象分析和设计》课程资料_课程注册系统_6-8 Identify Design Elements、RunTime Architecture、Describe Distribution.pdf
- 复旦大学:《面向对象分析和设计》课程资料_课程注册系统_9-11 UC Design、Subsystem Design、Class Design.pdf
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 1 - 面向对象分析和设计.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 2 - 迭代、进化和敏捷.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_OOP:Object-Oriented Programming.pptx
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 3 - 案例分析 THE NEXTGEN POS SYSTEM.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 4 Chapter 5 - 初始阶段的需求.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 6 - 用例.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 7 - 其他需求.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 8 - 细化阶段的迭代——基础.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 9 - 领域模型.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 10 - 系统顺序图.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 11 - 操作契约.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 13 - 逻辑架构和UML包图.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 14 - 迈向对象设计.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 15 - UML交互图.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 16 - UML类图.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 17 - GRASP基于职责设计对象.ppt
- 复旦大学:《面向对象分析和设计》课程资料_UML和模式_Chapter 18 - 使用GRASP的对象设计示例.ppt