JAVA异常

JAVA异常

AccountNotFoundException类

package com.blueleson.hello;
?
public class AccountNotFoundException extends Exception {
?
   public AccountNotFoundException(String message) {
       super(message);
   }
?
   @Override
   public String getMessage() {
       return "账号未找到";
   }
}

login类

package com.blueleson.hello;?public class User {    public void login(String account,String password)            throws AccountNotFoundException {        boolean accountExisted = false; // 认帐号不存在        String otherPassword;        // 此处可插入查询帐号的代码        if (accountExisted) { // 如果帐号不存在,抛出异常,程序中断            throw new AccountNotFoundException(account);       }   }    public static void main(String[] args) {        User user = new User();        try {            user.login("account","password");       } catch (AccountNotFoundException e) {            //插入处理帐号不存在的代码            System.out.println(e.getMessage());            System.exit(-1);       }        //插入登陆成功的代码        System.out.println("登陆成功!");   }?}

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...