异常模板代码
看這篇文章:http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html
再錄一下:
一個異常捕獲后,在?finally?里中再捕獲異常,拋出異常會覆蓋先前的異常信息,所以需要清晰的判斷每個可能的異常,如此不會遺漏被覆蓋的異常。如果代碼復雜,異常判斷是一鍵冗余的事情,所以會用模板的方式來簡化工作,減少出錯。
其實就是講一個共通的代碼如何更加優雅的公用。
Input input = null;IOException processException = null;try{input = new FileInputStream(fileName);//...process input stream...} catch (IOException e) {processException = e;} finally {if(input != null){try {input.close();} catch(IOException e){if(processException != null){throw new MyException(processException, e,"Error message..." +fileName);} else {throw new MyException(e,"Error closing InputStream for file " +fileName;}}}if(processException != null){throw new MyException(processException,"Error processing InputStream for file " +fileName;}
?
模板模式:
public abstract class InputStreamProcessingTemplate {public void process(String fileName){IOException processException = null;InputStream input = null;try{input = new FileInputStream(fileName);doProcess(input);} catch (IOException e) {processException = e;} finally {if(input != null){try {input.close();} catch(IOException e){if(processException != null){throw new MyException(processException, e,"Error message..." +fileName);} else {throw new MyException(e,"Error closing InputStream for file " +fileName;}}}if(processException != null){throw new MyException(processException,"Error processing InputStream for file " +fileName;}}//override this method in a subclass, to process the stream.public abstract void doProcess(InputStream input) throws IOException; }
調用:
new InputStreamProcessingTemplate(){public void doProcess(InputStream input) throws IOException{int inChar = input.read();while(inChar !- -1){//do something with the chars... }}}.process("someFile.txt");
?
接口實現的辦法:
public interface InputStreamProcessor {public void process(InputStream input) throws IOException; }public class InputStreamProcessingTemplate {public void process(String fileName, InputStreamProcessor processor){IOException processException = null;InputStream input = null;try{input = new FileInputStream(fileName);processor.process(input);} catch (IOException e) {processException = e;} finally {if(input != null){try {input.close();} catch(IOException e){if(processException != null){throw new MyException(processException, e,"Error message..." +fileName;} else {throw new MyException(e,"Error closing InputStream for file " +fileName);}}}if(processException != null){throw new MyException(processException,"Error processing InputStream for file " +fileName;}} }
調用:
new InputStreamProcessingTemplate().process("someFile.txt", new InputStreamProcessor(){public void process(InputStream input) throws IOException{int inChar = input.read();while(inChar !- -1){//do something with the chars... }}});
?
靜態模板:
public class InputStreamProcessingTemplate {public static void process(String fileName,InputStreamProcessor processor){IOException processException = null;InputStream input = null;try{input = new FileInputStream(fileName);processor.process(input);} catch (IOException e) {processException = e;} finally {if(input != null){try {input.close();} catch(IOException e){if(processException != null){throw new MyException(processException, e,"Error message..." +fileName);} else {throw new MyException(e,"Error closing InputStream for file " +fileName;}}}if(processException != null){throw new MyException(processException,"Error processing InputStream for file " +fileName;}} }
?
調用:
InputStreamProcessingTemplate.process("someFile.txt",new InputStreamProcessor(){public void process(InputStream input) throws IOException{int inChar = input.read();while(inChar !- -1){//do something with the chars...
}}}); ?
轉載于:https://www.cnblogs.com/killbug/p/4080606.html
總結
- 上一篇: 求下图出自哪部电影?
- 下一篇: Oracle中Hint深入理解(原创)