Java异常类

ApplicationException

/**
 * Wallstreet异常
 * 
 * @author liuxl
 *
 */
public class ApplicationException extends RuntimeException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 错误代码
	 */
	private String code;

	public ApplicationException() {
		super();
	}

	public ApplicationException(String msg) {
		super(msg);
	}

	public ApplicationException(Throwable th) {
		super(th);
	}

	public ApplicationException(String msg, Throwable th) {
		super(msg, th);
	}

	public ApplicationException(String code, String msg) {
		super(msg);
		this.code = code;
	}

	public ApplicationException(String code, String msg, Throwable th) {
		super(msg, th);
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}

 

InvalidParamException

/**
 * 输入参数校验异常
 * 
 * @author liuxl
 *
 */
public class InvalidParamException extends ApplicationException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 错误参数名称
	private String paramName;
	
	public InvalidParamException(String paramName) {
		super("Invalid param: " + paramName);
		this.setParamName(paramName);
	}

	public InvalidParamException(String msg, Throwable th) {
		super(msg, th);
	}

	public InvalidParamException(String code, String msg) {
		super(code, msg);
	}

	public String getParamName() {
		return paramName;
	}

	public void setParamName(String paramName) {
		this.paramName = paramName;
	}
	
}

 

EmptyResultException

/**
 * 没有查询到记录的异常
 * 
 * @author liuxl
 *
 */
public class EmptyResultException extends ApplicationException {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public EmptyResultException(String msg) {
		super(msg);
	}

	public EmptyResultException(Throwable th) {
		super(th);
	}

	public EmptyResultException(String msg, Throwable th) {
		super(msg, th);
	}

	public EmptyResultException(String code, String msg) {
		super(code, msg);
	}

	public EmptyResultException(String code, String msg, Throwable th) {
		super(code, msg, th);
	}
	
}

 

Exceptions

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 关于异常的工具类.
 * 
 * @author admin
 */
public class Exceptions {

    /**
     * 将CheckedException转换为UncheckedException.
     */
    public static RuntimeException unchecked(Exception e) {
        if (e instanceof RuntimeException) {
            return (RuntimeException) e;
        } else {
            return new RuntimeException(e);
        }
    }

    /**
     * 将ErrorStack转化为String.
     */
    public static String getStackTraceAsString(Exception e) {
        StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    /**
     * 判断异常是否由某些底层的异常引起.
     */
    public static boolean isCausedBy(Exception ex, Class<? extends Exception>... causeExceptionClasses) {
        Throwable cause = ex.getCause();
        while (cause != null) {
            for (Class<? extends Exception> causeClass : causeExceptionClasses) {
                if (causeClass.isInstance(cause)) {
                    return true;
                }
            }
            cause = cause.getCause();
        }
        return false;
    }
}

 

你可能感兴趣的:(Exception类封装)