java项目怎么定义异常_java项目经验总结之自定义异常类

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

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值