一种系统异常设计思路
来源:互联网 发布:使用u盘安装ubuntu 编辑:程序博客网 时间:2024/04/30 16:18
http://northconcepts.com/blog/2013/01/18/6-tips-to-improve-your-exception-handling/
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");
throw SystemException.wrap(e,PaymentCode.CREDIT_CARD_EXPIRED).set("fileName", "aaa");
- 一种系统异常设计思路
- WEB开发系统异常设计思路整理
- 又一种设计思路
- 一种手游中实时战斗系统的设计思路
- WEB开发系统异常设计思路整理,J2EE异常设计(原创)
- 跨国际链路的数据服务系统架构设计的一种实现思路
- 软件系统设计思路
- 软件系统设计思路
- 软件系统设计思路
- 软件系统设计思路
- 软件系统设计思路
- 软件系统设计思路
- 软件系统设计思路
- 一种权限系统设计
- 一种权限系统设计
- 一种Web UI 的代码设计思路
- 操作配置文件的一种设计思路
- 一种值得借鉴的设计思路
- poj2186 第一道Tarjan强连通缩点
- 科学之美:影响人类文明的方程式
- Android--- 利用Application实现全局数据共享
- oracle_error
- Servlet3.1规范翻译——变更历史
- 一种系统异常设计思路
- EventThread线程对VSync的接收
- windows下搭建php环境
- XmlDocument和LINQ to XML两种方式对xml的创建、查询
- JAVA通过反映方法实现C#的委托
- Sencha Touch Carousel 去除底栏
- 用户体验超棒并且功能强大使用简单的javascript表单验证 - Parsley.js
- 分享30套精美的Web和手机开发UI素材包
- Servlet3.1规范翻译——词汇表