当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PDF,文档页数:40,文件大小:1.24MB,团购合买
点击下载完整版文档(PDF)

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; } ?

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共40页,可试读14页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有