티스토리 뷰

JAVA/PRIMER

[JAVA] 예외처리

yulrang 2018. 4. 19. 14:14
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
import·java.util.Scanner;¬
¬
//···->·,·····¬
//···->·,···——————————(·error·)¬
//···->····,··(·exception·)¬
¬
public·class·1·{¬
————public·static·void·main(String[]·args)·{¬
————————Scanner·in·=·new·Scanner(System.in);¬
————————int·a·=·10;¬
————————¬
————————System.out.print("10····:·");¬
————————int·b·=·in.nextInt();¬
————————¬
————————//·try·:······¬
————————try{¬
————————————a·=·10·/b;——————————————————————//···!¬
————————————System.out.println(a);——————————//··¬
————————————¬
————————}catch(ArithmeticException·e)·{————//·¬
————————————//·catch·:······¬
————————————//·catch(··)·->····¬
————————————System.out.println("·!");¬
————————}¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10을 나눌 숫자 입력 : 0
오류 발생!

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
import·java.util.Scanner;¬
¬
¬
public·class·2·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————Scanner·in·=·new·Scanner(System.in);¬
————————int[]·arr·=·{1,2,3};¬
————————int·a·=·10;¬
————————¬
————————System.out.print("··:·");¬
————————int·b·=·in.nextInt();¬
¬
————————//····->·catch···¬
————————try{¬
————————————System.out.println(a/b);————//·b·0·¬
————————————System.out.println(arr[b]);//·b·4¬
————————}·catch(·ArithmeticException·e·){¬
————————————System.out.println("··!·0··.");¬
————————}·catch(·ArrayIndexOutOfBoundsException·e·){¬
————————————System.out.println("··!··3.");¬
————————}¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
정수 입력 : 10
1
배열 범위 오류! 배열의크기는 3입니다.


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
import·java.util.Scanner;¬
¬
¬
public·class·3·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————Scanner·in·=·new·Scanner(System.in);¬
————————int[]·arr·=·{1,2,3};¬
————————int·a·=·10;¬
————————int·b·=·in.nextInt();¬
¬
————————//····¬
————————try{¬
————————————System.out.println(a/b);————//·b·0·¬
————————————System.out.println(arr[b]);//·b·4¬
————————}·catch(·Exception·e·){¬
————————————System.out.println("!");¬
————————————System.out.println(·e·);¬
————————————System.out.println(·e.getMessage()·);¬
————————————e.printStackTrace();————————//····¬
————————}·finally{——//·······¬
————————————System.out.println("·!");¬
————————}¬
————————¬
————————//·Exception··:···super¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
정수 입력 : 10
1
오류!
java.lang.ArrayIndexOutOfBoundsException: 10
10
무조건 실행됨!
java.lang.ArrayIndexOutOfBoundsException: 10
at 예외처리3.main(예외처리3.java:17)


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
import·java.util.Scanner;¬
¬
class·A{¬
————static·void·method1(int·a){¬
————————method2(a);¬
————}¬
————static·void·method2(int·a){¬
————————method3(a);¬
————}¬
————static·void·method3(int·a){¬
————————System.out.println(10/a);¬
————}¬
}¬
public·class·4·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————Scanner·in·=·new·Scanner(System.in);¬
————————System.out.print("··:·");¬
————————int·a·=·in.nextInt();¬
————————¬
————————try·{¬
————————————A.method1(a);¬
————————}·catch(Exception·e){¬
————————————e.printStackTrace();————//······¬
————————}¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
정수 입력 : 0
java.lang.ArithmeticException: / by zero
at A.method3(에외처리4.java:11)
at A.method2(에외처리4.java:8)
at A.method1(에외처리4.java:5)
at 에외처리4.main(에외처리4.java:22)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//·······¬
//··1·)·try-catch·¬
//··2·)··throws·¬
public·class·7·{¬
¬
————public·static·void·main(String[]·args)·{¬
————————for(int·i=10;·i>=0;·i--){¬
————————————System.out.print(i+"·");¬
————————————try{¬
————————————————Thread.sleep(1000);¬
————————————}catch(Exception·e){¬
————————————————System.out.print("");¬
————————————}¬
————————}¬
————}¬
¬
}¬
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 9 8 7 6 5 4 3 2 1 0 


'JAVA > PRIMER' 카테고리의 다른 글

[JAVA] 파일입출력 - BufferedReader, BufferedWriter  (0) 2018.04.19
[JAVA] 파일입출력 - FileReader, FileWriter  (0) 2018.04.19
[JAVA] HashMap  (0) 2018.04.19
[JAVA] HashSet  (0) 2018.04.19
[JAVA] LinkedList  (0) 2018.04.19
댓글