//密碼錯(cuò)誤異常類
public class PasswordError extends Exception {
public PasswordError(String message){
super(message);
}
}
//用戶名錯(cuò)誤異常類
public class UserError extends Exception{
public UserError(String message){
super(message);
}
}
import java.util.Scanner;
public class RegisterException {
private static String userName = "12345678";
private static String passerWord = "123456";
public static void main(String[] args) throws UserError, PasswordError {
Scanner sc = new Scanner(System.in);
System.out.print("請輸入賬號:");
String str1 = sc.nextLine();
System.out.print("請輸入密碼:");
String str2 = sc.nextLine();
login(str1, str2);
}
private static void login(String userName, String passerWord) throws PasswordError, UserError {
if (!RegisterException.userName.equals(userName)){
//判斷用戶名是否輸入錯(cuò)誤
throw new UserError("用戶名錯(cuò)誤");//拋出用戶名錯(cuò)誤異常
}
if (!RegisterException.passerWord.equals(passerWord)){
//判斷密碼是否輸入錯(cuò)誤
throw new PasswordError("密碼錯(cuò)誤");//拋出密碼錯(cuò)誤異常
}
System.out.println("登錄成功");
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-698077.html
文章來源:http://www.zghlxwxcb.cn/news/detail-698077.html
到了這里,關(guān)于實(shí)現(xiàn)一個(gè)簡單的控制臺版用戶登陸程序, 程序啟動提示用戶輸入用戶名密碼. 如果用戶名密碼出錯(cuò), 使用自定義異常的方式來處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!