日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

异常模板代码

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 异常模板代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看這篇文章: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

總結

以上是生活随笔為你收集整理的异常模板代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。