微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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("登陆成功!");   }?}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐