
→ 붉은 색은 처리하지 않아도 된다.
public class ExceptionTest {
public static void main(String[] args) {
/*
1. Unchecked Exception
- RuntimeException 자손
- 저장하는 순간, 이클립스에서 컴파일 수행
- 일단 실행은 되지만, 컴파일 시점 이후에 확인된다.
- 컴파일러가 예외 처리를 강제하지 않는다.
*/
int[] num = {10};
System.out.println(num[2]);
/*
2. Checked Exception
- Exception 자손 중에서, RuntimeException 자손은 아닌 애들
- 붉은 에러 표시로 보여준다. -> 컴파일 자체가 수행되지 않았다.
- 실행이 불가능하다.
*/
Class.forName("Hello");
Thread.sleep(1000);
}
}

public static void main(String[] args) {
int[] nums = {10};
try {
System.out.println("1번 코드");
System.out.println(nums[2]); // 예외 발생 가능 코드
System.out.println("정상의 경우 출력");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("인덱스 범위 초과 에러");
} finally {
System.out.println("finally 출력");
}
System.out.println("프로그램 종료");
}
1번 코드
인덱스 범위 초과 에러
finally 출력
프로그램 종료
catch (AExceoption | BException e) { } 로 작성할 수 있다. 예외 객체에 해당하는 catch 문장을 찾을 때는 다형성 적용
상속 관계에서 상위 타입의 예외가 먼저 선언되는 경우, 뒤의 catch 블록은 동작할 기회가 없다.
상속 관계에서는 작은 범위에서 큰 범위 순으로 정의
예외 메세지를 확인하며, 프로그램 정상 종료 진행하는 방법
System.out.print(e.getMassage()); e.printStackTrace();catch에서 처리가 안 되는 예외가 있더라도, finally 구문은 진행 후 프로그램이 강제 종료가 된다.
try 메서드 내부에서 return; 구문을 작성하더라도, finally는 진행 후에 main 메서드가 종료된다.
public class TryCatchTest3 {
public static void main(String[] args) {
int[] nums = {10};
try {
System.out.println("1");
System.out.println(nums[0]);
System.out.println("2");
return;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("3");
} finally {
System.out.println("4");
}
System.out.println("프로그램 종료");
}
}
1 2 4
public class ThrowsTest {
public static void main(String[] args) {
try {
method1();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static void method1() throws ClassNotFoundException {
method2();
}
private static void method2() throws ClassNotFoundException {
Class.forName("Hello");
}
}
class Parent{
void methodA() throws IOException{ }
void methodB() throws ClassNotFoundException{ }
}
public class Child extends Parent {
@Override
void methodA() throws FileNotFoundException{ }
@Override
void methodB() throws Exception{ }
}
FileInputStream fis = null;
try {
fis = new FileInputStream("text.txt");
// 메인 로직 작성
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try(FileInputStream fis = new FileInpuStream("test.txt")) {
// 메인 로직 작성
} catch (IOException e) {
e.printStackTrace();
}
public class FruitNotFoundException extends Exception {
FruitNotFoundException(){}
FruitNotFoundException(String name){
// super : 현재 부모 class인 Exception의 생성자를 호출한다는 뜻
super(name + "에 해당하는 과일이 없습니다.");
}
}
public class UserExceptionTest {
public static void main(String[] args) {
try {
method1();
} catch (FruitNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void method1() throws FruitNotFoundException {
// 처리 진행 ... ing
// 예외 발생 !
// 예외를 직접 발생 시킬 때는 throw 키워드 사용
throw new FruitNotFoundException("사과");
}
}

throw 사용하면 throw declaration을 진행하라고 한다.

콘솔 창에 위와 같이 뜬다.
public class FruitNotFoundException2 extends RuntimeException {
FruitNotFoundException2(){}
FruitNotFoundException2(String name){
// super : 현재 부모 class인 Exception의 생성자를 호출한다는 뜻
super(name + "에 해당하는 과일이 없습니다.");
}
}
