一种系统异常设计思路
2013-02-07 15:15
218 查看
http://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/
1 系统只有一个异常基类继承RuntimeEcption,如果仅仅依靠异常子类的种类来区分,很容易造成类爆炸。
2 设置错误code,用枚举来保存,这样来区分异常类别,而且非常方便进行处理和国际化。
3 增加异常时参数设置,debug很容易找到当时的环境。
4 重写异常方法,抛出异常时打印出code和环境参数
5 正确包装其他异常,不用默认构造函数包装,防止异常无意义的嵌套。
包装异常
catch (IOException e) {
throw SystemException.wrap(e,PaymentCode.CREDIT_CARD_EXPIRED).set("fileName", "aaa");
1 系统只有一个异常基类继承RuntimeEcption,如果仅仅依靠异常子类的种类来区分,很容易造成类爆炸。
2 设置错误code,用枚举来保存,这样来区分异常类别,而且非常方便进行处理和国际化。
3 增加异常时参数设置,debug很容易找到当时的环境。
4 重写异常方法,抛出异常时打印出code和环境参数
5 正确包装其他异常,不用默认构造函数包装,防止异常无意义的嵌套。
package com.northconcepts.exception; import java.io.PrintStream; import java.io.PrintWriter; import java.util.Map; import java.util.TreeMap; public class SystemException extends RuntimeException { private static final long serialVersionUID = 1L; 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); } private ErrorCode errorCode; private final Map<String,Object> properties = new TreeMap<String,Object>(); public SystemException(ErrorCode errorCode) { this.errorCode = errorCode; } public SystemException(String message, ErrorCode errorCode) { super(message); this.errorCode = errorCode; } public SystemException(Throwable cause, ErrorCode errorCode) { super(cause); this.errorCode = errorCode; } public SystemException(String message, Throwable cause, ErrorCode errorCode) { super(message, cause); this.errorCode = errorCode; } public ErrorCode getErrorCode() { return errorCode; } public SystemException setErrorCode(ErrorCode errorCode) { this.errorCode = errorCode; return this; } public Map<String, Object> getProperties() { return properties; } @SuppressWarnings("unchecked") public <T> T get(String name) { return (T)properties.get(name); } public SystemException set(String name, Object value) { properties.put(name, value); return this; } //重写Runtime方法。这里打印虚拟机错误信息,注意PrintStream要同步,不然异常并非时会乱掉 public void printStackTrace(PrintStream s) { synchronized (s) { printStackTrace(new PrintWriter(s)); } } //把出异常时的参数也 打印出来,方便调试 public void printStackTrace(PrintWriter s) { synchronized (s) { s.println(this); s.println("\t-------------------------------"); if (errorCode != null) { s.println("\t" + errorCode + ":" + errorCode.getClass().getName()); } for (String key : properties.keySet()) { s.println("\t" + key + "=[" + properties.get(key) + "]"); } s.println("\t-------------------------------"); StackTraceElement[] trace = getStackTrace(); for (int i=0; i < trace.length; i++) s.println("\tat " + trace[i]); Throwable ourCause = getCause(); if (ourCause != null) { ourCause.printStackTrace(s); } s.flush(); } } }
public class SystemExceptionExample2 { private static final int MIN_LENGTH = 10; public static void main(String[] args) { validate("email", "abc"); } public static void validate(String field, String value) { if (value == null || value.length() < MIN_LENGTH) { throw new SystemException("发生错误!",ValidationCode.VALUE_TOO_SHORT) .set("field", field).set("value", value).set("min-length", MIN_LENGTH); } } }
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); }
包装异常
catch (IOException e) {
throw SystemException.wrap(e,PaymentCode.CREDIT_CARD_EXPIRED).set("fileName", "aaa");
相关文章推荐
- WEB开发系统异常设计思路整理,J2EE异常设计(原创)
- 跨国际链路的数据服务系统架构设计的一种实现思路
- WEB开发系统异常设计思路整理
- WEB开发系统异常设计思路整理,J2EE异常设计(原创)
- 系统设计 - IOS 程序插件及功能动态更新思路
- 简洁常用权限系统的设计与实现(七):一种错误的不可行的构造树的方法
- 基于时间序列的异常检测系统的实现思路之一
- 一种新的思路设计远控去改造小白虎远程控制软件
- 一种手游中实时战斗系统的设计思路
- 分库分表的设计思路让大系统变简单
- .NET 日志系统设计思路及实现代码
- B/S系统操作日志设计思路
- 针对有效的错误处理设计异常管理系统
- 更新系统固件的一种思路
- 商城系统商品属性的数据库设计思路
- 系统设计面试题思路综述
- 动态动作系统——一种全新的游戏人物动作制作思路
- 游戏任务系统设计思路
- 简洁常用权限系统的设计与实现(七):一种错误的不可行的构造树的方法
- 实现基于组织机构的数据集权限系统的设计思路讲解【提供完整数据库设计下载】