java项目经验总结之自定义异常类
java项目经验总结之自定义异常类
指尖残雪 发表于2年前
java项目经验总结之自定义异常类
  • 发表于 2年前
  • 阅读 10
  • 收藏 1
  • 点赞 2
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

在项目开发中遇到自定义异常,根据自己项目的需要进行相应的覆写,便于捕获和错误输出。

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());
		}
	}

}



共有 人打赏支持
粉丝 8
博文 73
码字总数 0
×
指尖残雪
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: