zoukankan      html  css  js  c++  java
  • java异常处理

    这里简单说一下,我在书中看到的关于java异常的处理。

    建议将try/catch和try/finally代码进行解耦,这样可以提高代码的清晰度。例如:

    InputStream in = ...;
    try {
        try {
            // code that might throw exceptions
        }
        finally {
            in.close();
        }
    } catch (IOException e) {
        // show error message
    }

    如果觉得上面写法嵌套太多,可以考虑改为如下:

    public class Handler {
        public boolean ReadData(String fileName) {
            try {
                return readDataImpl(fileName);
            } catch (IOException e) {
                // show error message
            }
        }
    
        private boolean readDataImpl(String fileName) throws IOException {
            InputStream in = ...;
            try {
                // code that might throw exceptions
            } finally {
                in.close();
            }
        }
    }

    如果对异常的处理模块一致的话,可以考虑重构一下上述的代码, 写成类似于下面的形式:

    public class Handler {
        public boolean ReadData(String fileName) {
            return manip(fileName->this.readDataImpl(fileName), fileName);
        }
    
        private boolean manip(Predicate<String> func, String fileName) {
            try {
                return func.test(fileName);
            } catch (IOException e) {
                // show error message
            }
        }
    
        private boolean readDataImpl(String fileName) throws IOException {
            InputStream in = ...;
            try {
                // code that might throw exceptions
            } finally {
                in.close();
            }
        }
    }    

    上面的ReadData的实现,使用了java8的lambda表达式。

    上述写法,在close函数抛出异常的情况下,会将原先的异常丢失,为了防止出现上述问题,可以考虑使用如下的代码形式:

    InputStream in = ...;
    Exception ex = null;
    try {
        try {
            // code that might throw exceptions
        } catch (Exception e) {
            ex = e;
            throw e;
        }
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            if (ex == null) throw e;
        }
    }

    从java7开始,可以使用带资源的try语句(try-with-resources), 使用方式如下:

    public class Handler {
        public boolean ReadData(String fileName) {
            return manip(fileName->this.readDataImpl(fileName), fileName);
        }
    
        private boolean manip(Predicate<String> func, String fileName) {
            try {
                return func.test(fileName);
            } catch (IOException e) {
                // show error message
            }
        }
    
        private boolean readDataImpl(String fileName) throws IOException {
         try (InputStream in = ...) { // code that might throw exceptions } } }

    这样就可以保证异常可以处理,而且实现代码和异常处理代码基本分离。

  • 相关阅读:
    HTTP请求下载文件格式
    MT7621 加 openWRT 用HTTP和远程服务器通信
    MT7621加 OPENWRT 移植MQTT(paho.mqtt.c) 进行数据的收发
    MT7621安装的openwrt出现无法删除文件的问题
    GAI_LIB = -lanl
    error: expected declaration specifiers or '...' before numeric constant void free(void *);
    environment variable 'STAGING_DIR' not defined
    ubuntu安装 make4.2
    gcc在root权限下查不到版本
    【原创】大叔经验分享(113)markdown语法
  • 原文地址:https://www.cnblogs.com/albizzia/p/10679796.html
Copyright © 2011-2022 走看看