java项目经验总结之自定义异常类
2016-02-08 16:25
691 查看
在项目开发中遇到自定义异常,根据自己项目的需要进行相应的覆写,便于捕获和错误输出。
单元测试:
package org.dyb.exception; import java.io.PrintStream; import java.io.PrintWriter; /** * @说明 自定义异常基础类 * @author dyb * */ public class BaseException extends RuntimeException{ private static final long serialVersionUID = -434104233779192938L; /** * 未知错误code */ public static final int UNKNOWN_ERROR_CODE = 0; /** * 未知错误message="" */ public static final String UNKNOWN_ERROR_MSG = ""; private Throwable cause;//异常 private int errorCode;//错误code private String traceId;//追踪id public BaseException(String errorMsg){ this(null, errorMsg); } public BaseException(Throwable cause){ this(cause, ""); } public BaseException(int errorCode, String errorMsg) { this(null, errorCode, errorMsg); } public BaseException(Throwable cause, String errorMsg){ this(cause, BaseException.UNKNOWN_ERROR_CODE, errorMsg); } public BaseException(Throwable cause, int errorCode, String errorMsg){ this(cause, errorCode, errorMsg, null); } public BaseException(Throwable cause, int errorCode, String errorMsg, String traceId){ super(errorMsg); this.cause = cause; this.errorCode = errorCode; this.traceId = traceId; } public void printStackTrace() { this.printStackTrace(System.err); } public void printStackTrace(PrintStream ps){ if(null == getCause()){ super.printStackTrace(ps); }else{ ps.println(this); getCause().printStackTrace(ps); } } public void printStackTrace(PrintWriter pw){ if(null == getCause()){ super.printStackTrace(pw); }else{ pw.println(this); getCause().printStackTrace(pw); } } public Throwable getCause(){ return this.cause == this ? null : this.cause; } public String getMessage(){ if (getCause() == null) { return super.getMessage(); } return super.getMessage() + getCause().getMessage(); } public int getErrorCode() { return errorCode; } public String getTraceId() { return traceId; } }
package org.dyb.exception; public class BoException extends BaseException{ private static final long serialVersionUID = 2729611264067131179L; public BoException(int errorCode, String errorMsg) { super(errorCode, errorMsg); } public BoException(String errorMsg) { super(errorMsg); } public BoException(Throwable cause, int errorCode, String errorMsg, String traceId) { super(cause, errorCode, errorMsg, traceId); } public BoException(Throwable cause, int errorCode, String errorMsg) { super(cause, errorCode, errorMsg); } public BoException(Throwable cause, String errorMsg) { super(cause, errorMsg); } public BoException(Throwable cause) { super(cause); } }
package org.dyb.exception; public class DaoException extends BaseException { /** * */ private static final long serialVersionUID = 1L; public DaoException(String errorMsg) { super(errorMsg); } public DaoException(int errorCode, String errorMsg) { super(errorCode, errorMsg); } public DaoException(Throwable cause, int errorCode, String errorMsg, String traceId) { super(cause, errorCode, errorMsg, traceId); } public DaoException(Throwable cause, int errorCode, String errorMsg) { super(cause, errorCode, errorMsg); } public DaoException(Throwable cause, String errorMsg) { super(cause, errorMsg); } public DaoException(Throwable cause) { super(cause); } }
package org.dyb.exception; public class ServiceException extends BaseException{ private static final long serialVersionUID = 8566572900818858358L; public ServiceException(int errorCode, String errorMsg) { super(errorCode, errorMsg); } public ServiceException(String errorMsg) { super(errorMsg); } public ServiceException(Throwable cause, int errorCode, String errorMsg, String traceId) { super(cause, errorCode, errorMsg, traceId); } public ServiceException(Throwable cause, int errorCode, String errorMsg) { super(cause, errorCode, errorMsg); } public ServiceException(Throwable cause, String errorMsg) { super(cause, errorMsg); } public ServiceException(Throwable cause) { super(cause); } }
单元测试:
package org.dyb.exception; import org.junit.Test; public class TestException { @Test public void test() { try { service(); } catch (ServiceException e) { System.out.println("test"+e.getMessage()); System.out.println("test"+e.getErrorCode()); } } public void service() throws ServiceException { try { bo(); } catch (BoException e) { throw new ServiceException(e.getErrorCode()," ---service:"+e.getMessage()); } } public void bo() throws BoException { try { dao(); } catch (DaoException e) { throw new BoException(e.getErrorCode()," ---bo:"+e.getMessage()); } } public void dao() throws DaoException { try { throw new DaoException(1,"aa"); } catch (DaoException e) { throw new DaoException(e.getErrorCode()," ----dao:"+e.getMessage()); } } }
相关文章推荐
- java 变长參数使用原则
- JAVA设计模式实战---代理模式!!!
- java学习--jvm内存初学
- java 双向链表实现序列
- Mybatis-Spring SqlSessionTemplate 源码解析
- 安装Eclipse for MAC 苹果版
- javaweb导出Excel数据与图片
- eclipse启动报错
- eclipse在Android xml布局中提示
- java的13种核心技术
- Java中HashMap遍历的两种方式
- eclipse配置安卓环境(mac系统)
- JAVA 内部类、外部类、继承、抽象类、接口实战
- spring ioc 容器概念
- Java中的设计模式
- 10. mybatis 高级: 整合spring
- Java多线程编程总结 java 1.6版
- springMvc 可传入的参数
- SpringMVC +hibernate4 集合中的疑问
- HDU-1039-Easier Done Than Said?(Java && 没用正則表達式是我的遗憾.....)