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 */} }