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
56
57
58
59
60
61
| package Java0324.Lecture13.Exception;
import java.util.Scanner;
/*
* 등록할 ID를 입력하세요. : hello1234
* ID는 최대 8글자만 가능합니다.
*
* 등록할 ID를 입력하세요. : hello123
* ID를 등록하였습니다.
*/
public class ExceptionMain06 {
static void register() throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("등록할 ID를 입력하세요.");
String id = sc.nextLine();
if(id.length() > 8)
throw new CheckIDException("ID는 최대 8글자만 가능합니다.");
System.out.println("ID를 등록하였습니다.");
}
public static void main(String[] args) {
try {
register();
} catch(Exception e) {
System.out.println(e.getMessage());
}
/*
Scanner sc = new Scanner(System.in);
System.out.println("등록할 ID를 입력하세요.");
String id = sc.nextLine();
try {
if(id.length() > 8) {
throw new CheckIDException("ID는 최대 8글자만 가능합니다.");
// throw new Exception("ID는 최대 8글자만 가능합니다.");
}
System.out.println("ID를 등록하였습니다.");
} catch(Exception e) {
e.printStackTrace();
// System.out.println(e.getMessage());
}
/*
if(id.length() > 8) {
System.out.println("ID는 최대 8글자만 가능합니다.");
} else {
System.out.println("ID를 등록하였습니다.");
}
*/
}
}
|