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