API를 사용할 때 설계자의 의도에 따라서 예외를 반드시 처리해야 하는 경우 ( 던진다 , 넘긴다)
ex 1)
import java.io.*;
public class CheckedExceptionDemo {
public static void main(String[] args) {
BufferedReader bReader = new BufferedReader(new FileReader("out.txt"));
String input = bReader.readLine();
System.out.println(input);
//out.txt 파일을 읽어서 그것을 화면에 출력하는 내용
}
}
에러 발생!!
이유 : 로직에 대한 예외처리가 필요하다
FileReader라는 클래스를 API문서를 보게 되면
FileReader의 생성자가 동작할 때 파일을 열 수 없는 경우가 생길 수 있고, 이런 경우 생성자 FileReader에서는 이 문제를 처리할 수 없기 때문에 이에 대한 처리를 생성자의 사용자에게 위임하겠다는 의미다
따라서 API의 사용자 쪽에서는 예외에 대한 처리를 반드시 해야 한다
import java.io.*;
public class CheckedExceptionDemo {
public static void main(String[] args) {
try {
BufferedReader bReader = new BufferedReader(new FileReader("out.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try{
String input = bReader.readLine();
} catch (IOException e){
e.printStackTrace();
}
System.out.println(input);
}
}
그래도 여전히 에러가 발생
이유 : 변수는 try의 중괄호 안에서 선언되어 있다. 위 try 의 bReader 를 아래 try의 bReader를 가져올 수 없다.
해결 방법
import java.io.*;
public class CheckedExceptionDemo {
public static void main(String[] args) {
BufferedReader bReader = null;
String input = null;
try {
bReader = new BufferedReader(new FileReader("out.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try{
input = bReader.readLine();
} catch (IOException e){
e.printStackTrace();
}
System.out.println(input);
}
}
throw
throw는 예외처리를 다음 사용자에게 넘기는 것
ex)
class B{
void run(){
}
}
class C{
void run(){
B b = new B();
b.run();
}
}
public class ThrowExceptionDemo {
public static void main(String[] args) {
C c = new C();
c.run();
}
}
B -> C -> Throw -> 일반사용자
B가 예외를 처리 할 수 있지만 C로 넘길 수 있고 C는 Throw 에게 넘길 수 있는 것 ~~
ex 2)
import java.io.*;
class B{
void run(){
BufferedReader bReader = null;
String input = null;
try {
bReader = new BufferedReader(new FileReader("out.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try{
input = bReader.readLine();
} catch (IOException e){
e.printStackTrace();
}
System.out.println(input);
}
}
class C{
void run(){
B b = new B();
b.run();
}
}
public class ThrowExceptionDemo {
public static void main(String[] args) {
C c = new C();
c.run();
}
}
이 코드의 예외를 넘기고 싶다면?
import java.io.*;
class B{
void run() throws IOException, FileNotFoundException{
BufferedReader bReader = null;
String input = null;
bReader = new BufferedReader(new FileReader("out.txt"));
input = bReader.readLine();
System.out.println(input);
}
}
class C{
void run(){
B b = new B();
try {
b.run();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ThrowExceptionDemo {
public static void main(String[] args) {
C c = new C();
c.run();
}
}
B.run 내부에서 IOException, FileNotFoundException에 해당하는 예외가 발생하면 이에 대한 처리를 B.run의 사용자에게 위임하는 것
또한 이것을 main으로 다시 넘길 수 있다.
import java.io.*;
class B{
void run() throws IOException, FileNotFoundException{
BufferedReader bReader = null;
String input = null;
bReader = new BufferedReader(new FileReader("out.txt"));
input = bReader.readLine();
System.out.println(input);
}
}
class C{
void run() throws IOException, FileNotFoundException{
B b = new B();
b.run();
}
}
public class ThrowExceptionDemo {
public static void main(String[] args) {
C c = new C();
try {
c.run();
} catch (FileNotFoundException e) {
System.out.println("out.txt 파일은 설정 파일 입니다. 이 파일이 프로잭트 루트 디렉토리에 존재해야 합니다.");
} catch (IOException e) {
e.printStackTrace();
}
}
}