* 프로그램 오류 종류
▶ 컴파일 에러 - 컴파일할 때 발생하는 에러
런타임 에러 - 실행할 때 발생하는 에러
▶ Java의 런타임 에러(error) 와 예외(exception)
◎ 에러(error) - 프로그램 코드에 의해서 수습될 수 없는 심각한 오류
◎ 예외(Exception) - 프로그램 코드에 의해서 수습될 수 있는 다소 미약한 오류
정의 - 프로그램 실행 시 발생할 수 있는 예외의 발생에 대한 코드를 작성하는것
목적 - 프로그램의 비정상 종료를 막고, 정상적인 실행 상태를 유지하는것
** 에러와 예외는 모두 실행 시(runtime) 발생하는 오류이다.
-> 예외처리구문 - try - catch
- 예외를 처리하려면 try-catch문을 사용해야 함
try {
// 예외가 발생할 가능성이 있는 문장을 넣는다.
} catch (Exception1 e1) {
// Exception1이 발생했을 경우, 이를 처리하기 위한 문장을 적는다.
} catch (Exception2 e2) {
// Exception2이 발생했을 경우, 이를 처리하기 위한 문장을 적는다.
}
//** if문과 달리 try블럭이나 catch 블럭 내에 포함된 문장이 하나라고 해서 괄호{} 생략할 수 없다.
try-catch문에서의 흐름
▶ try블럭 내에서 예외가 발생한 경우
1. 발생한 예외와 일치하는 catch블럭이 있는지 확인
2. 일치하는 chach 블럭을 찾게 되면, 그 catch블럭 내의 문장을 수행하고 전체 try-catch 문을 빠저나가
다음 문장을 계속해서 수행, 만일 일치하는 catch 블럭을 찾지 못하면 예외처리는 되지 못한다.
▶ try블럭 내에서 예외가 발생하지 않는 경우
1. catch블럭을 거치지 않고 전체 try-catch문을 빠져나가서 수행을 계속한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
package ExceptionEx6;
public class ExceptionEx4 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(4);
} catch (Exception e){
System.out.println(5);
}
System.out.println(6);
}
}
// try문장에서 예외가 발생하지 않은 catch 블럭은 실행되지 않는다.
|
cs |
-> 예외 발생 시키기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
package ExceptionEx6;
public class ExceptionEx5 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(0/0);
System.out.println(4);
// 고의로 바로 윗줄에서 에러가 발생하여 실행되지 않음
} catch (Exception e) {
System.out.println(5);
} //try-catch문의 끝
System.out.println(6);
}
} |
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
package ExceptionEx6;
public class ExceptionEx6 {
public static void main(String[] args) {
try {
Exception e = new Exception("고의로 발생시켰음");
throw e;
//throw new Exception("고의로 발생시켰음"); 한줄로 사용가능
} catch (Exception e) {
System.out.println("에러메시지 : " + e.getMessage());
e.printStackTrace();
}
System.out.println("프로그램이 정상 종료되었음.");
}
}
// Exception 생성자에 String을 넣어주면 String이 Exception인스턴스
// 메시지로 저장되며 이때 getMessage() 메서들을 이용하여 얻을 수 있다. |
cs |
* Runtime Exection 종류
예외 타입 |
설명 |
ArithmeticException |
어떤 수를 0으로 나누는 것과 같이 비정상 계산 중 발생 |
NullPointerException |
NULL 객체 참조시 발생 |
IllegalArgumentException |
메소드의 전달 인자값이 잘못될 경우 발생 |
IllegalStateException |
객체의 상태가 메소드 호출에는 부적합할 경우 발생 |
IndexOutOfBoundsException |
배열의 index 값이 범위를 넘어갈 경우 발생 |
UnsupportedOperationException |
객체가 메소드를 지원하지 않은 경우 발생 |
SecurityException |
보안 위반 발생 시 보안 관리 프로그램에서 발생 |
ProviderException |
구성 공급자 오류시 발생 |
NoSuchElementException |
구성요소가 그 이상 없는 경우 발생 |
ArrayStoreException |
객체 배열에 잘못된 객체 유형 저장시 발생 |
ClassCastException |
클래스 간의 형 변환 오류시 발생 |
EmptyStackException |
스택이 비어있는데 요소를 제거하려고 할시 발생 |
ClassNotFoundException |
지정된 클래스가 없는 경우 발생 |
FileNotFoundException |
존재하지 않는 파일의 이름을 입력했을 경우 |
DataFormatException |
입력한 데이터 형식이 잘못된 경우 발생 |
Exception클래스들 - 사용자의 실수와 같은 외적인 요인에 의해 발생하는 예외
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
package ExceptionEx6;
public class ExceptionEx7 {
public static void main(String[] args) {
try {
throw new Exception();// Exception을 강제로 발생시킨다.
} catch (Exception e) {
System.out.println("Exception이 발생했습니다.");
}
}
}
// Exception 클래스들은 반드시 예외처리를 해주어야 한다.(컴파일 자체가 안됨)
// runtime Exception클래스들은 예외처리를 하지 않아도 컴파일러가 문제를 삼지 않음 |
cs |
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 |
package ExceptionEx6;
public class ExceptionEx11 {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
try {
System.out.println(3);
System.out.println(0/0);
System.out.println(4);
}// 연산자가 instance 연산을 하여 ArithmeticException true인 경우 실행
catch (ArithmeticException ae) {
if (ae instanceof ArithmeticException) {
System.out.println("true");
System.out.println("AritheticException");
}
//ArithmeticException 제외한 모든 예외과 처리된다.
} catch (Exception e){
System.out.println("Exception");
}
System.out.println(6);
}
}
//instanceof 연산자는 만들어진 객체가 특정 클래스의 인스턴스
// 인지를 물어보는 연산자 결과로 boolean 값을 리턴한다. |
cs |
'JAVA' 카테고리의 다른 글
용어정리~ (0) | 2015.10.26 |
---|---|
멀티 스레드 VS 멀티 프로세스 (0) | 2015.10.26 |
자바의 객체지향개념 2-3 (0) | 2015.10.18 |
자바의 객체지향개념 2-2 (0) | 2015.10.05 |
자바의 객체지향개념 2-1 (0) | 2015.10.05 |