-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathException16.java
More file actions
55 lines (46 loc) · 1.72 KB
/
Exception16.java
File metadata and controls
55 lines (46 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-09-15 10:06:50
* @LastEditTime: 2020-09-15 10:20:59
* @FilePath: \java\Exception\Exception16.java
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
*/
import javax.security.auth.login.LoginException;
//package Exception;
public class Exception16 {
//// 模拟数据库中已存在账号
private static String[] names = {"bill", "dssd", "dsds"};
public static void main(String[] args) {
//调用方法
try{
// 可能出现异常的代码
checkUsername("nill");
System.out.println("注册成功");//如果没有异常就是注册成功
} catch(LoginException | RegisterException e){
//处理异常
e.printStackTrace();
}
}
//判断当前注册账号是否存在
//因为是编译期异常,又想调用者去处理 所以声明该异常
public static boolean checkUsername(String uname) throws LoginException, RegisterException {
for (String name : names) {
if(name.equals(uname)){//如果名字在这里面 就抛出登陆异常
throw new RegisterException("亲"+name+"已经被注册了!");
}
}
return true;
}
}
// 业务逻辑异常
class RegisterException extends Exception{
public RegisterException() {
}
public RegisterException(String message) {
super(message);
}
}