改善异常处理的 6 个技巧

在软件开发过程中,往往会出现一些不可预知的错误,这些错误有可能花费你数小时甚至数天的时间。异常处理通常是防止这种未知错误的常用措施,它的好处是你不用再绞尽脑汁去考虑各种错误,这为处理某一类错误提供了一个很有效的方法,使编程效率大大提高。

本文将为你介绍6个技巧,来帮助你提高异常处理的能力。

1.  使用一个单一的、系统范围的异常类

不要针对每种异常类型创建单独的类,而是只创建一个,并使它继承RuntimeException。这可以减少类的数量,并移除你不会去处理的需要声明的异常。

我知道你在想什么:如何告诉异常处理程序,这些是否是同一类型?如何跟踪特定类型的属性?继续读下去。

2.  为错误代码使用枚举

大多数开发者会把导致异常的原因放到消息中,出现异常时,查看日志文件即可。但是这也有一些缺点:

  • 消息不能被翻译
  • 消息不能很容易地映射为易读的文本
  • 无法从程序方面对消息进行检查

由于每个开发者的语言习惯不一样,同样的错误可能有不同的描述形式。


更好的办法是使用枚举来表示异常的类型。针对每种错误创建一个枚举,并使枚举实现一个ErrorCode接口,然后将它引用为异常中的一个字段。

当抛出异常时,只需在适当的枚举中传递即可。

Java代码 复制代码
  1. throw new SystemException(PaymentCode.CREDIT_CARD_EXPIRED);  
throw new SystemException(PaymentCode.CREDIT_CARD_EXPIRED);


现在,当你需要测试具体情况时,只需比较异常代码即可。

Java代码 复制代码
  1. catch (SystemException e) {   
  2.   if (e.getErrorCode() == PaymentCode.CREDIT_CARD_EXPIRED) {   
  3.   ...   
  4.   }   
  5. }  
catch (SystemException e) {
  if (e.getErrorCode() == PaymentCode.CREDIT_CARD_EXPIRED) {
  ...
  }
}


在资源包中使用错误代码作为关键字进行检索,即可取回易读的、国际化的文本。

Java代码 复制代码
  1. public class SystemExceptionExample3 {   
  2.     
  3.     public static void main(String[] args) {   
  4.         System.out.println(getUserText(ValidationCode.VALUE_TOO_SHORT));   
  5.     }   
  6.     
  7.     public static String getUserText(ErrorCode errorCode) {   
  8.         if (errorCode == null) {   
  9.             return null;   
  10.         }   
  11.         String key = errorCode.getClass().getSimpleName() + "__" + errorCode;   
  12.         ResourceBundle bundle = ResourceBundle.getBundle("com.northconcepts.exception.example.exceptions");   
  13.         return bundle.getString(key);   
  14.     }   
  15.     
  16. }  
public class SystemExceptionExample3 {
 
    public static void main(String[] args) {
        System.out.println(getUserText(ValidationCode.VALUE_TOO_SHORT));
    }
 
    public static String getUserText(ErrorCode errorCode) {
        if (errorCode == null) {
            return null;
        }
        String key = errorCode.getClass().getSimpleName() + "__" + errorCode;
        ResourceBundle bundle = ResourceBundle.getBundle("com.northconcepts.exception.example.exceptions");
        return bundle.getString(key);
    }
 
}


3.  在枚举类型中添加错误代码

在某些情况下,一个数字形式的错误代码可以对应一个异常,例如HTTP响应。在这种情况下,在ErrorCode接口中添加一个getNumber方法,并在每个枚举类型中实现它。

Java代码 复制代码
  1. public enum PaymentCode implements ErrorCode {   
  2.   SERVICE_TIMEOUT(101),   
  3.   CREDIT_CARD_EXPIRED(102),   
  4.   AMOUNT_TOO_HIGH(103),   
  5.   INSUFFICIENT_FUNDS(104);   
  6.     
  7.   private final int number;   
  8.     
  9.   private PaymentCode(int number) {   
  10.     this.number = number;   
  11.   }   
  12.     
  13.   @Override  
  14.   public int getNumber() {   
  15.     return number;   
  16.   }   
  17.     
  18. }  
public enum PaymentCode implements ErrorCode {
  SERVICE_TIMEOUT(101),
  CREDIT_CARD_EXPIRED(102),
  AMOUNT_TOO_HIGH(103),
  INSUFFICIENT_FUNDS(104);
 
  private final int number;
 
  private PaymentCode(int number) {
    this.number = number;
  }
 
  @Override
  public int getNumber() {
    return number;
  }
 
}



这些数字可以是全局唯一的,或者每个枚举类型只对应一个数字。你甚至可以使用隐式ordinal()方法,或者从一个文件/数据库中加载一些数字。

4.  将动态字段添加到异常处理中

好的异常处理也意味着记录相关数据,而不仅仅是堆栈跟踪。这样做会节省你大量用于诊断和重现错误的时间。当你的程序停止工作时,也无需客户告诉你,他们做了什么。

做到这一点最简单的方法是在异常处理中增加一个java.util.Map字段。该字段主要作用是保留所有异常相关的数据。如果你使用fluent interface(连贯接口)模式,你还需要添加一个通用的setter方法。

抛出异常,并带有相关的数据,类似于下面的形式:

Java代码 复制代码
  1. throw new SystemException(ValidationCode.VALUE_TOO_SHORT)   
  2.   .set("field", field)   
  3.   .set("value", value)   
  4.   .set("min-length", MIN_LENGTH);  
throw new SystemException(ValidationCode.VALUE_TOO_SHORT)
  .set("field", field)
  .set("value", value)
  .set("min-length", MIN_LENGTH);


5.  防止不必要的嵌套

长且多余的堆栈跟踪,对谁都没有好处。更糟的是,它们浪费你的时间和资源。当重新抛出异常时,调用一个静态封装方法,而不是异常的构造函数。封装方法将决定何时嵌套异常,以及何时返回原来的实例。

Java代码 复制代码
  1. public static SystemException wrap(Throwable exception, ErrorCode errorCode) {   
  2.   if (exception instanceof SystemException) {   
  3.     SystemException se = (SystemException)exception;   
  4.     if (errorCode != null && errorCode != se.getErrorCode()) {   
  5.       return new SystemException(exception.getMessage(), exception, errorCode);   
  6.     }   
  7.     return se;   
  8.   } else {   
  9.     return new SystemException(exception.getMessage(), exception, errorCode);   
  10.   }   
  11. }   
  12.     
  13. public static SystemException wrap(Throwable exception) {   
  14.   return wrap(exception, null);   
  15. }  
public static SystemException wrap(Throwable exception, ErrorCode errorCode) {
  if (exception instanceof SystemException) {
    SystemException se = (SystemException)exception;
    if (errorCode != null && errorCode != se.getErrorCode()) {
      return new SystemException(exception.getMessage(), exception, errorCode);
    }
    return se;
  } else {
    return new SystemException(exception.getMessage(), exception, errorCode);
  }
}
 
public static SystemException wrap(Throwable exception) {
  return wrap(exception, null);
}


重新抛出异常的代码类似于:

Java代码 复制代码
  1. catch (IOException e) {   
  2.   throw SystemException.wrap(e).set("fileName", fileName);   
  3. }  
} catch (IOException e) {
  throw SystemException.wrap(e).set("fileName", fileName);
}


6.  使用一个有Web控制面板的中央记录器

根据你的情况,访问产品日志可能会相当麻烦。因为这可能会涉及到多个中间人。

如果你在一个多服务器环境中,事情会更糟。找到出问题的服务器,并确定该问题只影响这一台服务器,这可能相当令人头痛。
我的建议是:

  • 将日志聚合到某一位置,最好是数据库
  • 允许从Web浏览器访问该数据库

有很多方法可以做到这一点,比如:日志收集器、远程记录器、JMX代理、系统监控软件等,你甚至可以自己构建一个。一旦你拥有它,你将能够:

  • 在几秒钟内解决问题。
  • 每个异常都有一个URL对应。
  • 维护人员无需通过其他人的帮助即可确定异常原因
  • 防止测试人员为同样的错误创建多个tickets。
  • 为企业节省资金
  • 节省你的时间,不会影响你的周末度假

异常处理示例下载:NorthConcepts-Exceptions.zip
http://www.iteye.com/news/27078

猜你喜欢

转载自tdcq.iteye.com/blog/1777094