您的位置:首页 > 编程语言 > Java开发

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: