java_自定义异常

public class MyException extends RuntimeException {
	private String errorCode;
	public MyException(String message) {
		super(message);
	}
	public MyException(String errorCode,String message) {
		super(message);
		this.errorCode = errorCode;
	}
	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}
	public String getErrorCode() {
		return errorCode;
	}
}

public static void main(String[] args) {
  try {
    if (true) {
        throw new MyException("9999","业务异常出现了");
    }
  } catch (MyException e) {
     System.out.println(e.getErrorCode()+e.getMessage());
  }
}

猜你喜欢

转载自blog.csdn.net/maqingbin8888/article/details/82590630