public class Test2_Exception2 {

       public static void main(String[] args) {

              f();//模拟用户登录

       }

 

       //模拟用户登录

       private static void f() {

              System.out.println("输入用户名:");

              String name = new Scanner(System.in).nextLine();

             

              System.out.println("输入密码:");

              String pwd = new Scanner(System.in).nextLine();

              try {

                     login(name,pwd);

                     System.out.println("欢迎登录...");

              } catch (UserNameNFException e) {

                     System.out.println("用户名错误");

              } catch (PwdNFException e) {

                     System.out.println("密码错误");

              }

       }

 

       //设置用户名abc密码123

       private static void login(String name, String pwd)

                                                 throws UserNameNFException, PwdNFException {

              if(!"abc".equals(name)) {

                     throw new UserNameNFException();

              }

              if(!"123".equals(pwd)) {

                     throw new PwdNFException();

              }

       }

}

Q.E.D.


努力成就未来