步骤1,自定义一个异常类,该类继承RuntimeException,并在该类中放置错误码,该错误码要与在国际化消息文本相同。
package com.bjsxt.struts;
public class ErrorCodeException extends RuntimeException {
private String errorCode;
private Object[] args;
//错误码中无占位符的构造方法
public ErrorCodeException(String errorCode) {
this(errorCode, null);
}
//存在占位符的构造方法
public ErrorCodeException(String errorCode, Object args0) {
this(errorCode, new Object[]{args0});
}
public ErrorCodeException(String errorCode, Object[] args) {
this.errorCode = errorCode;
this.args = args;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getArgs() {
return args;
}
}
步骤2:在需要抛出异常的地方使用该异常
步骤3:配置异常