package chapter05;
public class Exception_oop2 { public static void main(String[] args) throws Exception { try { login("zhangsan","123"); } catch(AccountException accountException){ System.out.println("账号错误,请重新登录!!"); } catch(PasswordException passwordException){ System.out.println("密码错误,请重新登录!!"); } catch(LoginException loginException){ System.out.println("其他错误,请重新登录!!"); }
}
public static void login(String account , String password){ if(!"admin".equals(account)){ throw new AccountException("账号错误!"); } if(!"admin".equals(password)){ throw new PasswordException("密码错误!"); } } }
class AccountException extends LoginException{ public AccountException(String message){ super(message); } }
class PasswordException extends LoginException{ public PasswordException(String message){ super(message); } }
class LoginException extends RuntimeException{
public LoginException(String message){ super(message); } }
|