-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThrowTest.java
More file actions
48 lines (42 loc) · 973 Bytes
/
Copy pathThrowTest.java
File metadata and controls
48 lines (42 loc) · 973 Bytes
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
package java_base;
class MyException extends Exception{
private int idnumber;
public MyException(String message, int id)
{
super(message);
this.idnumber = id;
}
public int getIdnumber() {
return idnumber;
}
}
public class ThrowTest {
private void regist(int num) throws MyException {
// TODO Auto-generated method stub
if(num < 0) {
throw new MyException("人工抛出异常", 3);
}
System.out.println("登记人数:" + num);
}
private void manager() {
// TODO Auto-generated method stub
try {
regist(2);
} catch (MyException e) {
// TODO: handle exception
System.out.println("登记出错"+e.getIdnumber());
}finally {
System.out.println("over");
}
}
public static void main(String[] args) {
ThrowTest t = new ThrowTest();
t.manager();
}
}
/*
* 上一次实验室 检测异常
* 这一次是自定义抛出异常 在方法隔壁 thows 异常类 在方法里面 thow 异常类
* 捕获的是一个类 存放在e里面
* 自定义异常类 有个String message 需要传进来
*/