📢 예외(Exception)
- 정상적이지 않은 케이스
📢 예외 처리(Exception Handling)
- 정상적이지 않은 case에 대한 적절한 처리 방법
try{
int a = 5 / 0;
}
catch (ArithmeticException e)
{
System.out.println("0으로 나누기 예외 발생");
System.out.println("e = " + e);
}
📢 Finally
- 예외 발생과 상관없이 항상 실행되는 부분
/**
try{
int a = 5 / 0;
}
catch (ArithmeticException e)
{
System.out.println("0으로 나누기 예외 발생");
System.out.println("e = " + e);
}
*/
finally {
System.out.println("1-1 연습 종료");
}
📢 throw
- 개발자가 의도적으로 예외를 발생시키는 것
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 void main(String[] args) throws IOException {
System.out.println("== checkTenWithException ==");
checkResult = Main.checkTenWithException(9);
System.out.println("checkResult = " + checkResult);
}
📢 throws
- 메서드 내에서 예외처리를 하지 않고, 해당 메서드를 사용한 곳에서 예외처리하도록 하는 것
- 예외처리를 전가하는 것이다.
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("== checkTenWithThrows ==");
try{
checkResult = Main.checkTenWithThrows(9);
}catch (NotTenException e)
{
System.out.println("e = " + e);
}
System.out.println("checkResult = " + checkResult);
}
반응형
'Java' 카테고리의 다른 글
[Java] 콘솔 입출력 (0) | 2023.08.06 |
---|---|
[Java] 배열의 의미와 종류 (0) | 2023.08.05 |
[Java] 여러 가지 연산자 (0) | 2023.08.04 |
댓글