1. catch 블록을 사용한 복합 에러처리
- catch 블록은 에러가 예상되는 상황에 대해 복수로 명시하는 것이 가능하다.
1-1. Exception Class
- Java에서 예외 상황을 의미하는 모든 클래스들의 최상위 클래스
- 이 클래스의 이름으로 catch 블록을 구성하면, 모든 예외 상황에 일괄적으로 대응할 수는 있지만, catch 블록이세분화 된 경우와는 달리 상황 별 개별적인 처리는 불가능하다.
- Exception 클래스에 대한 예외처리는 대부분 맨 마지막 catch 블록에 명시하여 '마지막 알 수 없는 에러'
를 의미하도록 구성한다.
try {
....
} catch ( NumberFprmatException e ) {
....
} catch ( ArrayIndexOutOfException e ) {
....
} catch ( Exception e ) {
....
}
try {
....
} catch ( NumberFprmatException | ArrayIndexOutOfException e ) {
....
} catch ( Exception e ) {
....
}
1-2. 에러 객체 'e' 의 기능
- e.getMessage( )
-> 간략한 에러 메시지를 리턴한다.
-> e.getLocaliseMessage( ) 도 같은 기능을 한다.
- e.printStackTrace( )
-> 실제 예외 상황시에 출력되는 메세지를 강제로 출력한다.
-> 개발자가 catch 블록 안에서 예외 상황을 분석하기 위한 용도로 사용한다.
file
package file;
import java.io.File;
public class Main01 {
public static void main(String[] args) {
File file = new File("src/file/Main01.java");
boolean is_file = file.isFile();
System.out.println("is_file : " + is_file);
boolean is_dir = file.isDirectory();
System.out.println("is_dir : " + is_dir);
boolean is_hidden = file.isHidden();
System.out.println("is_hidden : " + is_hidden);
String abs = file.getAbsolutePath();
System.out.println("절대경로 : " + abs);
boolean is_exist = file.exists();
System.out.println("존재 여부 : " + is_exist);
System.out.println("-----------------------");
File file2 = new File("a/b/c/target");
System.out.println("isFile : " + file2.isFile());
System.out.println("isDirectory : " + file2.isDirectory());
System.out.println("isHidden : " + file2.isHidden());
System.out.println("절대경로 : " + file2.getAbsolutePath());
System.out.println("존재여부 : " + file2.exists());
file2.mkdirs();
System.out.println("---------------------");
System.out.println(file.getName());
System.out.println(file2.getName());
System.out.println(file.getParent());
System.out.println(file2.getParent());
boolean del_ok = file2.delete();
System.out.println("삭제 성공 여부 : " + del_ok);
}
}
package file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class Main02 {
public static void main(String[] args) {
String path = "text.txt";
String write_string = "오늘피곤하구만abcdefg";
byte[] buffer = null;
try {
buffer = write_string.getBytes ("utf-8");
} catch (UnsupportedEncodingException e) {
System.out.println("[ERROR] 알 수 없는 인코딩 정보>> " + path);
e.printStackTrace();
}
OutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(buffer);
System.out.println("[INFO]파일 저장 성공 >> " + path);
} catch (FileNotFoundException e) {
System.out.println("[ERROR] 지정된 경로를 찾을 수 없음>> " + path);
e.printStackTrace();
} catch (IOException e) {
System.out.println("[ERROR] 파일 저장 실패 >> " + path);
e.printStackTrace();
} catch (Exception e) {
System.out.println("[ERROR] 알 수 없는 에러>> " + path);
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
public class Main03 {
public static void main(String[] args) {
String path = "text.txt";
byte[] data = null;
String read_string = null;
InputStream in = null;
try {
in = new FileInputStream(path);
data = new byte[in.available()];
in.read(data);
System.out.println("[INFO] 파일 읽어오기 성공 >> " + path);
} catch (FileNotFoundException e) {
System.out.println("[ERROR] 지정된 경로를 찾을 수 없음 >> " + path);
e.printStackTrace();
} catch (IOException e) {
System.out.println("[ERROR] 파일 읽기 실패 >> " + path);
e.printStackTrace();
} catch (Exception e) {
System.out.println("[ERROR] 알 수 없는 에러 >> " + path);
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (data != null) {
try {
read_string = new String(data, "utf-8");
System.out.println(read_string);
} catch (UnsupportedEncodingException e) {
System.out.println("[ERROR] 인코딩 지정 에러 >> " + path);
e.printStackTrace();
}
}
}
}