예외
- 정상적이지 않은 Case
- 0으로 나누기, 배열의 인덱스 초과, 없는 파일 열기 등
int a = 1 / 0; >> 실행 시 에러 발생!
예외 처리
- 정상적이지 않은 Case에 대한 적절한 처리 방법
try {
...
} catch (예외 case 1) {
... // case 1일 경우 실행
} catch (예외 case 2) {
... // case 2일 경우 실행
}
int a = 0;
try {
a = 5 / 0;
} catch (ArtihmeticException e) {
System.out.println(e);
}
finally
- 예외 발생 여부와 관계없이 항상 실행되는 부분
try {
예외가 발생할 수도 있는 부분;
} catch (예외 case 1) {
예외 case1이 발생해야 실행되는 부분;
} finally {
항상 실행되는 부분;
}
throw, throws
- throw: 예외를 발생 시킴
- throws: 예외를 전가 시킴
... 함수이름 () {
throw new Exception();
}
... 함수이름() throws Exception {
...
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class NotTenException extends RuntimeException {}
public class Main {
public static boolean checkTen(int ten) {
if (ten != 10) {
throw new NotTenException();
}
return true;
}
public static boolean checkTenWithException(int ten) {
try {
if (ten != 10) {
throw new NotTenException();
}
} catch (NotTenException e) {
System.out.println("e = " + e);
return false;
}
return true;
}
public static boolean checkTenWithThrows(int ten) throws NotTenException {
if (ten != 10) {
throw new NotTenException();
}
return true;
}
public static void main(String[] args) throws IOException {
System.out.println("== 0으로 나누기 ==");
try {
int a = 5 / 0;
} catch ( ArithmeticException e ) {
System.out.println("0으로 나누기 예외 발생");
System.out.println("e = " + e);
} finally {
System.out.println("1-1 연습 종료");
}
System.out.println("== 배열 인덱스 초과 ==");
int[] b = new int[4];
try {
b[4] = 1;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("e = " + e);
}
System.out.println("== 없는 파일 열기 ==");
System.out.println("== checkTen ==");
boolean checkResult = Main.checkTen(10);
System.out.println("checkResult = " + checkResult);
System.out.println("== checkTenWithException ==");
checkResult = Main.checkTenWithException(9);
System.out.println("checkResult = " + checkResult);
System.out.println("== checkTenWithThrows ==");
try {
checkResult = checkTenWithThrows(5);
} catch (NotTenException e) {
System.out.println("e = " + e);
}
System.out.println("checkResult = " + checkResult);
}
}