platformexception.java

来自「这是本人曾经在公司里用的,内部开发框架,基于struts+hibernate今天」· Java 代码 · 共 104 行

JAVA
104
字号
/**
 * 
 */
package cn.bway.common;

/**
 * @author Kson
 *
 */
public class PlatformException extends Exception {

	private String errMsg = "";

	/**
	 * 这个构造函数禁止被外界调用
	 */
	private PlatformException() {
	}

	/**
	 * 构造函数
	 * @param e Exception
	 */
	public PlatformException(Exception e) {
		if (e instanceof PlatformException) {
			this.errMsg = ((PlatformException) e).getErrMsg();
		} else {
			StackTraceElement[] elements = e.getStackTrace();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < elements.length; i++) {
				sb.append(((StackTraceElement) elements[i]).toString());
				sb.append("\n");
			}
			sb.append(e.toString());
			this.errMsg = sb.toString();
		}
	}

	/**
	 * 构造函数
	 * @param errStr String
	 * @param e Exception
	 */
	public PlatformException(String errStr, Exception e) {
		StringBuffer sb = new StringBuffer();
		sb.append(errStr);
		sb.append("\n");
		if (e instanceof PlatformException) {
			sb.append(((PlatformException) e).getErrMsg());
			this.errMsg = sb.toString();
		} else {
			StackTraceElement[] elements = e.getStackTrace();
			for (int i = 0; i < elements.length; i++) {
				sb.append(((StackTraceElement) elements[i]).toString());
				sb.append("\n");
			}
			sb.append(e.toString());
			this.errMsg = sb.toString();
		}
	}

	/**
	 * 构造函数
	 * @param errStr String
	 */
	public PlatformException(String errStr) {
		this.errMsg = errStr;
	}

	/**
	 *重载toString方法
	 * @return String 转换为显示字符串
	 */
	public String toString() {
		return errMsg;
	}

	/**
	 * 获取错误信息
	 * @return String
	 */
	public String getErrMsg() {
		return this.errMsg;
	}

	/**
	 * 设置错误消息
	 * @param errMsg String
	 */
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}

	public static void main(String[] args) {
		//        try {
		//            int i = 3 / 0;
		//        } catch (Exception e) {
		//            PlatformException pe = new PlatformException(e);
		//            System.out.println("msg=" + pe.getErrMsg());
		//        }
	}

}

⌨️ 快捷键说明

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