-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentTest.java
More file actions
45 lines (40 loc) · 1.02 KB
/
Copy pathStudentTest.java
File metadata and controls
45 lines (40 loc) · 1.02 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
package random.java;
public class StudentTest {
public static void main(String[] args) {
Student s = new Student();
try {
s.regist(-1001);
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student{
private int id;
// public void regist(int id) throws Exception{
// if(id > 0){
// this.id = id;
// }else{
//// System.out.println("您输入的数据非法!");
//
// /*手动抛出异常*/
// throw new Exception("您输入的数据非法!");
// }
// }
public void regist(int id){
if(id > 0){
this.id = id;
}else{
// System.out.println("您输入的数据非法!");
/*手动抛出异常*/
throw new MyException("禁止输入负数!");
}
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
'}';
}
}