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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584 |
------------------------------------------------------------------------------------------
package chap05;
public class ClassCastExceptionClass {
public static void main(String[] args) {
ClassCastSubClass cs;
ClassCastSuperClass css;
ClassCastSuperClass css1;
try {
cs = new ClassCastSubClass();
css = cs;
css.print();
cs.print();
// css1=(ClassCastSuperClass1)cs;//IS A의 관계 아님
// cs=(ClassCastSuperClass)css;//ClassCastSuperClass 상위 클래스
Object ob = css;
// css=(Object)ob;//Object 상위 클래스 임
css = (ClassCastSuperClass) ob;
css.print();
// if(css instanceof ClassCastSuperClass1){
// css1=css;
// }
Object ob1 = new Object();
String st = new String("test");
// String str=(String)ob1;
if (st instanceof Object) {
ob1 = st;
}
if (ob1 instanceof String) {
String str = (String) ob1;
}
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("프로그램 종료");
}
}
class ClassCastSuperClass {
int a = 5;
public void print() {
System.out.println(a);
}
}
class ClassCastSubClass extends ClassCastSuperClass {
int a = 10;
public void print() {
System.out.println(a);
}
}
class ClassCastSuperClass1 {
int a = 5;
public void print() {
System.out.println(a);
}
}
------------------------------------------------------------------------------------------
package chap05;
public class CreateInvoke extends SuperExceptionClass {
public static void main(String[] args) {
CreateInvoke i = null;
try {
i = new CreateInvoke();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
System.out.println("프로그램 종료");
}
}
CreateInvoke() throws ArithmeticException, Exception {
}
}
class SuperExceptionClass extends Exception {
public SuperExceptionClass() throws Exception {
throw new Exception("Super Exception");
}
}
------------------------------------------------------------------------------------------
package chap05;
import java.io.*;
import java.io.FileNotFoundException;
public class FileFindException {
public static void main(String[] args) {
FileFindRead fr = null;
String str = "";
try {
File f = new File("D:/test.txt");
// File이라는 클래스를 통해서 파일존재 여부를 팡악할 수 있음.
if (f.exists() && f.isFile()) {
// f.exeist는 파일이 있는냐라고 물어본다.
// f.isFile은 해당파일이 파일인지 폴더인지 확인해준다.
// 맞으면 True값을 return
System.out.println("파일존재");
fr = new FileFindRead(new FileInputStream("D:/test.txt"));
// FileInputStram을 호출해서 FileFindRead라는 생성자를 호출한다.
} else {
System.out.println("파일 존재 하지 않음");
}
} catch (FileNotFoundException e) {
System.err.println(e.toString());
}
try {
str = fr.readLine();
} catch (IOException e) {
System.err.println(e.toString());
} finally {
try {
fr.fis.close();
} catch (Exception e) {
System.err.println(e.toString());
}
}
System.out.println(str);
}
}
class FileFindRead {
FileInputStream fis;
FileFindRead(FileInputStream fis) {
// FileFindRead 생성자의 매개변수인 FileInputStream 변수명 fis를 멤버변수 fis에 넣어줌
this.fis = fis;
}
public String readLine() throws IOException {
String str = "", st;
BufferedReader br = null;
if (fis != null) {
br = new BufferedReader(new InputStreamReader(fis));
// InputStreamReader가 없으면 한글이 깨진다.
while ((st = br.readLine()) != null) {
System.out.println(str);
str += st;
}
throw new IOException();
}
return str;
}
}
------------------------------------------------------------------------------------------
package chap05;
import java.io.*;
public class GuGuDan {
int num;
public static void main(String[] args) {
GuGuDan ml = new GuGuDan();
try {
System.out.println("2~9사이의 숫자만 입력합니다.");
boolean f = true;
do {
try {
if (ml.readNumber()) {
f = false;
ml.print(ml.num);
} else {
throw new NumberFormatException("2~9사이 숫자");
}
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}
} while (f);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void print(int dan) {
System.out.printf("-----%d단-----%n", dan);
for (int i = 1; i <= 9; i++) {
System.out.printf("%d * %d = %d %n", dan, i, dan * i);
}
System.out.println("-------------");
}
public boolean readNumber() throws IOException, NumberFormatException {
boolean f = false;
BufferedReader br = null;
String st = null;
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("원하는 단수를 입력하시오: ");
st = br.readLine();
int num = Integer.parseInt(st);
if (num >= 1 && num <= 9) {
f = true;
this.num = num;
}
br.close();
return f;
}
}
------------------------------------------------------------------------------------------
package chap05;
import java.io.*;
public class InputNumberLoopException {
int num;
public static void main(String[] args) {
InputNumberLoopException il = new InputNumberLoopException();
try {
System.out.println("1~10사이의 숫자만 입력합니다.");
boolean f = true;
do {
try {
if (il.readNumber()) {
f = false;
System.out.println(il.num * 10);
}
} catch (NumberFormatException e) {
}
} while (f);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public boolean readNumber() throws IOException, NumberFormatException {
boolean f = false;
BufferedReader br = null;
String st = null;
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("숫자를 입력해주세요:");
st = br.readLine();
int num = Integer.parseInt(st);
if (num >= 1 && num <= 10) {
f = true;
this.num = num;
}
br.close();
return f;
}
}
------------------------------------------------------------------------------------------
package chap05;
public class MethodInvoke {
public static void main(String[] args) {
String result = "undone";
try {
MethodInvoke invoke = new MethodInvoke();
result = invoke.method();
} catch (UserException2 ue) {
System.err.println(ue.getReason());
} finally {
System.err.println("finally =" + result);
}
}
public String method() throws UserException2 {
if (true)
throw new UserException2("User Reason");
return ("method done.");
}
}
class UserException2 extends Exception {
private String reason;
public UserException2(String cause) {
reason = cause;
}
public String getReason() {
return reason;
}
}
------------------------------------------------------------------------------------------
package chap05;
public class MethodOverridingException extends MethodOverringSuperClass {
public static void main(String[] args) {
MethodOverridingException m;
m = new MethodOverridingException();
try {
System.out.println("-----1-----");
m.method();
System.out.println("-----2-----");
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
System.out.println("프로그램 종료");
}
}
protected String method() throws ArithmeticException {
if (true)
new ArithmeticException("하위클래스 method");
return "하위클래스";
}
}
class MethodOverringSuperClass {
protected String method(String ar) throws Exception {
if (true)
new Exception("상위 클래스 Method");
return "상위클래스";
}
}
------------------------------------------------------------------------------------------
package chap05;
public class NotYet {
public static void main(String[] args) {
AssertTest at = new AssertTest();
at.method();// false이면 오류 발생
}
}
class AssertTest {
boolean value = false;
void method() {
assert value;
System.out.println("AssertTest.method()");
}
}
------------------------------------------------------------------------------------------
package chap05;
import java.io.*;
public class NumberFormatExceptionClass {
public static void main(String[] args) throws IOException {
BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in
// 표준입력 : 키보드 입력
));
String st = "";
System.out.println("숫자를 입력하세요");
st = br.readLine();
System.out.println(st);
new Atype(new Btype());
int num = 0;
try {
num = Integer.parseInt(st);
} catch (NumberFormatException e) {
System.out.println("숫자를 다시 입력하세요:");
st = br.readLine();
} finally {
System.out.println(num * 10);
}
}
}
class Atype {
Atype(Btype b) {
}
}
class Btype {
}
------------------------------------------------------------------------------------------
package chap05;
public class StackTrace {
public static void main(String[] args) {
try {
StackTrace trace = new StackTrace();
trace.method();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("exception: " + e.getMessage());
e.printStackTrace();
}
}
public void method() {
int[] array = new int[2];
System.out.println(array[2]);
}
}
// throw 예외상황을 던져주는 것
// throws는 예외상황을
------------------------------------------------------------------------------------------
package chap05;
public class ThrowException {
public static void main(String[] args) {
new ThrowExceptionClass().print();
}
}
class ThrowExceptionClass {
public void print() {
System.out.println("예외상황발생시키기");
int a = 5, b = 0, c = 0;
try {
c = a / b;
throw new Exception("예외상황 발생 :예외객체 생성");
} catch (ArithmeticException e) {
System.out.println("ArithmeticException:" + e.toString());
// throw new Exception("예외상황 발생: 예외객체 생성")
} catch (NullPointerException e) {
System.out.println("NullPounterException:" + e.toString());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException:" + e.toString());
} catch (Exception e) {
System.out.println("Exception:" + e.toString());
} finally {
System.out.println("exception 처리 종료");
}
}
}
------------------------------------------------------------------------------------------
package chap05;
public class ThrowsException {
public static void main(String[] args) {
ThrowsExceptionClass te = new ThrowsExceptionClass();
try {
te.print();
} catch (Exception e) {
e.printStackTrace();
} // printStackTrace란? 에러를 출력하는 역할.
try {
int a = te.print(3);
System.out.println(a);
} catch (Exception e) {
e.printStackTrace();
}
try {
String st = te.print("예외처리");
System.out.println(st);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class ThrowsExceptionClass {
public void print() throws Exception {
System.out.println("예외상황 Throws 하기");
throw new Exception("예외상황 객체생성");
}
public int print(int a) throws Exception {
try {
System.out.println("예외상황 Throws 하기1");
throw new Exception("예외상황 객체생성");
} catch (Exception e) {
System.out.println("예외상황 Throw Exception 처리");
} finally {
System.out.println("예외상황 Throws finally 블록 수행");
}
return a * 10;
}
public String print(String str) throws Exception {
String reStr = "테스트:" + str;
int a = 5, b = 0, c = 0;
System.out.println("예외상황 Throws 하기2");
c = a / b;
return reStr;
}
}
------------------------------------------------------------------------------------------
package chap05;
public class ThrowsSample {
public static void main(String[] args) {
ThrowsSampleClass t = new ThrowsSampleClass();
System.out.println("print 메소드 수행 전");
try {
t.print();
} catch (Exception e) {
System.out.println(e.toString());
}
System.out.println("print 메소드 수행 후");
System.out.println("print1 메소드 수행 전");
// try{
// t.print1();
// }catch(Exception e){}
System.out.println("프로그램 정상종료");
}
}
class ThrowsSampleClass {
public void print() throws ArithmeticException, NullPointerException {
int c = 0;
c = 5 / 0;
}
public void print1() {
int c = 0;
c = 5 / 0;
}
}
------------------------------------------------------------------------------------------
package chap05;
public class UserException extends Exception {
// Exception 클래스를 상속 받아야만 Exception이 된다.
private String exceptionName;
public static void main(String[] args) {
UserException ue;
try {
throw new UserException("내가만든 예외처리");
// throw new TestException("내가만든 예외처리")
// 실행할 수 없는 이유는 TestException은
// Exception 상속을 받지 않았기 때문에 Exception이 아니다.
// System.out.println(ue.getName());
} catch (UserException e) {// 하위클래스에서 예외처리르 받는다.
System.out.println(e.getName());
// getName()은 이름을 호출하는 것이며,
// 여기서는 String 타입인 "내가만든 예외처리"를 실행한다.
} catch (Exception e) {// 하위클래스에서 예외처리를 받을 수 없을 경우,
// 상위클래스에서 예외처리를 받는다.
System.out.println(e.toString());
// Exception의 경우 상위클래스가 Object이기 때문에 toString을 쓴 것이다.
} finally {
System.out.println("예외처리 종료");
}
System.out.println("프로그램 정상종료");
}
public UserException(String exceptionName) {
this.exceptionName = exceptionName;
}
public String getName() {
return exceptionName;
}
}
class TestException {
private String exceptionName;
public TestException(String exceptionName) {
this.exceptionName = exceptionName;
}
public String getName() {
return exceptionName;
}
}
------------------------------------------------------------------------------------------ |
cs |
'Legend 개발자 > Java' 카테고리의 다른 글
Read Code(Chap.07) (0) | 2017.07.20 |
---|---|
Read Code(Chap.06) (0) | 2017.07.20 |
Read Code(Chap.04) (0) | 2017.07.20 |
Read Code(Chap.03) (0) | 2017.07.20 |
Read Code(Chap.02) (0) | 2017.07.20 |