加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JAVA异常

发布时间:2020-12-15 07:43:27 所属栏目:Java 来源:网络整理
导读:JAVA异常 AccountNotFoundException类 package com.blueleson.hello; ? public class AccountNotFoundException extends Exception { ? ?? public AccountNotFoundException(String message) { ?????? super(message); ?? } ? ?? @Override ?? public String

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读