凡操作失败都应该抛出某种异常,为了便于管理自定义的异常,应该先创建这些异常的基类,在创建异常要生成【无参构造】方法,如下一样:
快捷键是:
代码区域->右键->source->GenerateConstructors from Supperclass…无参数构造方法alt+shift+s+c
//ServiceException.凡操作失败都应该抛出某种异常,为了便于管理自定义的异常,应该先创建这些异常的基类
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = -2879099986352308425L;
public ServiceException() {
super();
}
public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
public ServiceException(String message) {
super(message);
}
public ServiceException(Throwable cause) {
super(cause);
}
}
//UsernameDuplicateException 本次“注册”之前,需要先检查用户名是否已经被占用,如果已经被占用,则需要抛出“用户名被占用”的异常
public class UsernameDuplicateException extends ServiceException {
private static final long serialVersionUID = 3164055183124220212L;
public UsernameDuplicateException() {
super();
}
public UsernameDuplicateException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public UsernameDuplicateException(String message, Throwable cause) {
super(message, cause);
}
public UsernameDuplicateException(String message) {
super(message);
}
public UsernameDuplicateException(Throwable cause) {
super(cause);
}
}
//本次“注册”之前,需要先检查用户名是否已经被占用,如果已经被占用,则需要抛出“用户名被占用”的异常
public class InsertException extends ServiceException{
/**
*
*/
private static final long serialVersionUID = 878421569126542322L;
public InsertException() {
super();
}
public InsertException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public InsertException(String message, Throwable cause) {
super(message, cause);
}
public InsertException(String message) {
super(message);
}
public InsertException(Throwable cause) {
super(cause);
}
}
public class UserNotFoundException extends ServiceException{
/**
* 此次执行修改之前,需要检查用户数据是否存在,及是否被标记为“已删除”,则可能抛出UserNotFoundException;
*/
private static final long serialVersionUID = 129945205934194859L;
public UserNotFoundException() {
super();
}
public UserNotFoundException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public UserNotFoundException(String message) {
super(message);
}
public UserNotFoundException(Throwable cause) {
super(cause);
}
}
public class PasswordNotMatchException extends ServiceException{
/**
* 在执行修改之前,还会检查原密码是否正确,如果原密码错误,则可能抛出PasswordNotMatchException;
*/
private static final long serialVersionUID = 7750209971477940813L;
public PasswordNotMatchException() {
super();
}
public PasswordNotMatchException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public PasswordNotMatchException(String message, Throwable cause) {
super(message, cause);
}
public PasswordNotMatchException(String message) {
super(message);
}
public PasswordNotMatchException(Throwable cause) {
super(cause);
}
}
public class UpdateException extends ServiceException{
/**
* 在执行修改时,执行的是Update类型的操作,则可能抛出UpdateException
*/
private static final long serialVersionUID = 6612586132242913331L;
public UpdateException() {
super();
}
public UpdateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public UpdateException(String message, Throwable cause) {
super(message, cause);
}
public UpdateException(String message) {
super(message);
}
public UpdateException(Throwable cause) {
super(cause);
}
}