[JAVA] 예외처리(try-catch, throw, finally)란?
예외상황(Exception handling)이란?
코드를 작성하다가 보면 많은 오류가 등장합니다.
오류가 날 때 에러(error)를 다른 방식으로 처리하고 싶을 때 적절한 방식으로 처리하는 것을 예외상황이라고 합니다.
예외상황 관련된 용어
Throwing an Exception
어떤 일이 일어났을 때 던져지는 코드
Handling an Exception / Catching an Exception
예외 상황에 응답(대응)하는 코드
예외상황을 다룰 때는 try-catch문을 사용합니다.
try {
< code to try >
if (test condition) {
throw new Exception("Message to display");
< more code >
} catch(Exception e) {
<Exception handling code>
}
- try block에는 normal case가 들어갑니다.
- catch block에는 exceptional case가 들어갑니다.
- catch block에 parameter의 type은 Exception입니다.
- try block에서 Exception이 던져(throw)지면 catch block으로 넘어가게 됩니다.
예외상황 예제 코드
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try {
System.out.println("Enter number of donuts: ");
int donutCount = s.nextInt();
System.out.println("Enter number of glasses of milk: ");
int milkCount = s.nextInt();
// throw statement
if(milkCount<1) throw new Exception("Exception: No milk!");
double donutsPerGlass = donutCount/(double)milkCount;
System.out.println(donutCount + " donuts.");
System.out.println(milkCount + " glasses of milk.");
System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk.");
} catch(Exception e) {
System.out.println(e.getMessage());
System.out.println("Go buy some milk.");
}
System.out.println("End of program.");
}
코드를 하나하나 살펴보겠습니다.
try-block 입니다.
System.out.println("Enter number of donuts: ");
int donutCount = s.nextInt();
System.out.println("Enter number of glasses of milk: ");
int milkCount = s.nextInt();
우선 도넛과 우유의 개수를 사용자에게 입력 받습니다.
if(milkCount<1) throw new Exception("Exception: No milk!");
우유가 있으면 그대로 아래 코드가 수행되고 우유가 없다면 Exception을 던져 catch block으로 넘어가게 됩니다.
Exception이 던져(throw)되지 않은 경우에는
double donutsPerGlass = donutCount/(double)milkCount;
System.out.println(donutCount + " donuts.");
System.out.println(milkCount + " glasses of milk.");
System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk.");
이와 같은 출력이 나옵니다.
우유가 없어서 exception이 던져(throw)진 경우에는
catch(Exception e) {
System.out.println(e.getMessage());
System.out.println("Go buy some milk.");
}
Catch block의 코드가 수행됩니다.
Finally 구문
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
try {
System.out.println("Enter number of donuts: ");
int donutCount = s.nextInt();
System.out.println("Enter number of glasses of milk: ");
int milkCount = s.nextInt();
// throw statement
if(milkCount<1) throw new Exception("Exception: No milk!");
double donutsPerGlass = donutCount/(double)milkCount;
System.out.println(donutCount + " donuts.");
System.out.println(milkCount + " glasses of milk.");
System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk.");
} catch(Exception e) {
System.out.println(e.getMessage());
System.out.println("Go buy some milk.");
} finally {
System.out.println("\nEnd of program.");
}
}
위에 코드와 같기 때문에 finally 구문만 보면 됩니다.
finally 구문은 exception이 던져지든 아니든 실행되는 구문입니다.
Predefined Exception Class
- IOException
- ClassNotFoundException
- FileNotFoundException
- ArrayIndexOutOfBoundsException
등이 있습니다.
Exception은 모든 exception의 root 클래스입니다.
◡̈