class UserError extends Exception{
public UserError(String message) {
super(message);
}
}
class PasswordError extends Exception{
public PasswordError(String message) {
super(message);
}
}
public class TestDemo {
private static final String password = "199775";
private static final String userName = "yaoxinyuan";
public static void main(String[] args) {
try {
login("yaoxinyuan", "199775");
} catch (PasswordError passwordError) {
passwordError.printStackTrace();
} catch (UserError userError) {
userError.printStackTrace();
}
}
public static void login(String userName, String password) throws UserError, PasswordError {
if (!TestDemo.userName.equals(userName)) {
throw new UserError("用户名错误");
}
if (!TestDemo.password.equals(password)) {
throw new PasswordError("密码错误");
}
System.out.println("登录成功");
}
}
java自定义异常,实现用户登录程序
最新推荐文章于 2022-08-05 09:33:42 发布