国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

異常模板代碼

2019-11-14 21:48:34
字體:
供稿:網(wǎng)友
異常模板代碼

看這篇文章:http://tutorials.jenkov.com/java-exception-handling/exception-handling-templates.html

再錄一下:

一個異常捕獲后,在finally里中再捕獲異常,拋出異常會覆蓋先前的異常信息,所以需要清晰的判斷每個可能的異常,如此不會遺漏被覆蓋的異常。如果代碼復雜,異常判斷是一鍵冗余的事情,所以會用模板的方式來簡化工作,減少出錯。

其實就是講一個共通的代碼如何更加優(yōu)雅的公用。

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

調(diào)用:

  new InputStreamProcessingTemplate(){        public void doProcess(InputStream input) throws IOException{            int inChar = input.read();            while(inChar !- -1){                //do something with the chars...            }        }    }.process("someFile.txt");

接口實現(xiàn)的辦法:

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

調(diào)用:

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...                }            }        });

靜態(tài)模板:

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

調(diào)用:

 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...                }            }        });


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 独山县| 阿拉善左旗| 绥滨县| 左云县| 邵阳市| 定远县| 得荣县| 开江县| 蕲春县| 绥棱县| 马关县| 泰兴市| 禄丰县| 来宾市| 庆安县| 黄浦区| 黄石市| 镇原县| 香河县| 吉林市| 弋阳县| 新宁县| 沭阳县| 苗栗市| 罗平县| 荆门市| 临汾市| 通道| 香格里拉县| 丰都县| 富阳市| 黄龙县| 武平县| 辉南县| 资溪县| 汤原县| 通辽市| 深水埗区| 赣州市| 荣昌县| 浙江省|