标签:style blog java color 数据 io re c
import java.util.HashMap; import java.util.Map; public class LoginService{ private Map<String,String> map; public LoginService(){ map=new HashMap<String,String>(); map.put("张三", "123"); map.put("李四", "123"); map.put("王五", "123"); map.put("赵六", "1234"); map.put("孙七", "1234"); } public boolean login(String name,String password) throws NoUserException,ErrorPswdException{ if(map.containsKey(name)){ String pswd=map.get(name); if(pswd.equals(password)==false){ throw new ErrorPswdException("用户名"+name+"的密码不是"+password); } }else{ throw new NoUserException("DB里没有这个用户名"); } return true; } public static void main(String[] args){ LoginService service=new LoginService(); String name="张三1"; String pswd="1235"; try{ boolean passed=service.login(name,pswd); System.out.println("欢迎"+name+"登录系统."); }catch(NullPointerException e){ System.out.println("数据库未就绪,请通知系统管理员."); }catch(NoUserException e){ System.out.println("用户名"+name+"不存在,即将前往注册页面"); }catch(ErrorPswdException e){ System.out.println("用户的"+name+"密码"+pswd+"不正确,请明确后再输入"); }catch(Exception e){ e.printStackTrace(); } } }
两个异常类:
public class ErrorPswdException extends Exception{ public ErrorPswdException(String msg){ super(msg); } }
public class NoUserException extends Exception{ public NoUserException(String msg){ super(msg); } }
爪哇国新游记之十一----用异常控制流程,布布扣,bubuko.com
标签:style blog java color 数据 io re c
原文地址:http://www.cnblogs.com/xiandedanteng/p/3867441.html