appexception.java

来自「struts+hibernate开发的一个系统」· Java 代码 · 共 129 行

JAVA
129
字号
package org.appfuse.util.exception;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.PrintStream;
import java.io.PrintWriter;

public abstract class AppException extends Exception {
    /** Exception Error Code */
    private int errorCode = ExceptionConstants.SUCCESS;
    /** Exception ID */
    private String exceptionId;
    /** Exception Cause */
    protected Throwable cause;

    /**
     * Construct Exception with error code.
     *
     * @param errorCode int
     */
    public AppException(int errorCode) {
        super();
        this.errorCode = errorCode;
        genExceptionId();
    }

    /**
     * Construct Exception with message and error code.
     * @param message exception indication message
     * @param errorCode the corresponding error code
     */
    public AppException(String message, int errorCode) {
        super(message);
        this.errorCode = errorCode;
        genExceptionId();
    }

    /**
     * Construct exception with nested cause and error code.
     * @param cause the nested exception
     * @param errorCode the error code
     */
    public AppException(Throwable cause, int errorCode) {
        super(cause);
        this.cause = cause;
        this.errorCode = errorCode;
        genExceptionId();
    }

    /**
     * Construct exception with cause, message and error code.
     * @param message exception indication message
     * @param cause the nested cause
     * @param errorCode the error code
     */
    public AppException(String message, Throwable cause, int errorCode) {
        super(message, cause);
        this.errorCode = errorCode;
        this.cause = cause;
        genExceptionId();
    }

    /**
     * Get exception error code
     * @return the exception error code
     */
    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * Generate exception id.
     */
    public void genExceptionId() {
        String idFormat = "yyMMddHHmmssS";
        SimpleDateFormat sdf = new SimpleDateFormat(idFormat);
        this.exceptionId = sdf.format(new Date());
    }

    public String getExceptionId() {
        return exceptionId;
    }

    public void setExceptionId(String exceptionId) {
        this.exceptionId = exceptionId;
    }

    /**
     * Print Exception Stack trace.
     * @param s PrintStream
     */
    public void printStackTrace(PrintStream s) {
        s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
            "</exception_id>\r\n");
        s.println("\t<exception-localized-message>" + "ErrorCode=" +
            getErrorCode() + " LocalizedMessage=" +
            getLocalizedMessage() +
            "</exception-localized-message>\r\n\t<exception_stack_trace>");

        //if (cause != null) {
        //    cause.printStackTrace(s);
        //}

        super.printStackTrace(s);
        s.println("\t</exception_stack_trace>\r\n</app_exception>");
    }

    public void printStackTrace(PrintWriter s) {
        s.println("<app_exception>\r\n\t<exception_id>" + exceptionId +
            "</exception_id>\r\n");
        s.println("\t<exception-localized-message>" + "ErrorCode=" +
            getErrorCode() + " LocalizedMessage=" +
            getLocalizedMessage() +
            "</exception-localized-message>\r\n\t<exception_stack_trace>");

        //if (cause != null) {
        //    cause.printStackTrace(s);
        //}

        super.printStackTrace(s);
        s.println("\t</exception_stack_trace>\r\n</app_exception>");
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?