피드로 돌아가기
새로워지기·서른의 생활코딩

ex26) java_oop

NS
normalstory
표지 이미지




//예외 처리

//이 프로그램은 컴파일러에 의해 에러가 발생된다.
//즉 컴파일러는 a.txt 파일이 없을 경우에 발생하는 예외의
//처리를 요구한다
 
/*
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
FileReader file = new FileReader("a.txt");
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {
System.out.print((char)i);
}
file.close();
}
}**/




/*
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
try{
FileReader file = new FileReader("a.txt");
// 만일 a.txt 파일이 없다면?
}
int i;
while((i = file.read()) != -1) {
System.out.print((char)i);
}
file.close();
}
}
**/


/*
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
try{
FileReader file = new FileReader("a.txt");//IOException
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {//IOException
System.out.print((char)i);
}
file.close();//IOException
}catch (Exception e) {
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}
}
}//Exception:오류처리를 위한,.

**/




/*
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
try{
FileReader file = new FileReader("a.txt");//FileNotFoundException
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {//IOException
System.out.print((char)i);
}
file.close();//IOException
}catch (Exception e) {
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("해당파일없음");
}catch (IOException e) {
// TODO: handle exception
System.out.println("입출력오류");
}
}
}//Exception:오류처리를 위한,.
**/




/*
// 여러 catch code block을 사용하기위해서는 !!! -> 상속의 역순으로 작성해야한다!
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
try{
FileReader file = new FileReader("a.txt");   // Exception 객체 생성 ->FileNotFoundException
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {             // IOException
System.out.print((char)i);
}
file.close();//IOException
}catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("해당파일이없음");        // -> 여기로와서 수행하고 프로그램 종료!!!!
}catch (IOException e) {
// TODO: handle exception
System.out.println("입출력오류");
}catch (Exception e) {                        
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}
}
}//Exception:오류처리를 위한,.
**/



/*
// 만약 1번이 아닌 2번의 오류인 경우 2번으로 바로 실행.
// 단, 이때 file.close();가 실행되지않고 ->1번의 파일이 오픈된다. 
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
try{
FileReader file = new FileReader("a.txt");   // Exception 객체 생성 ->FileNotFoundException
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {             // IOException
System.out.print((char)i);
}
file.close();//IOException
}catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("해당파일이없음");        // -> 여기로와서 수행하고 프로그램 종료!!!!
}catch (IOException e) {
// TODO: handle exception
System.out.println("입출력오류");
}catch (Exception e) {                        
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}finally{
System.out.println("오류발생 유무와 관계없이 무조건 수행");
try{file.close();} catch (IOException e) {}
}
}
}//Exception:오류처리를 위한,.
**/





/*
import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
FileReader file=null; //지역변수니까 초기화
try{
file = new FileReader("a.txt");   
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {             // IOException
System.out.print((char)i);
}
}catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("해당파일이없음");        // -> 여기로와서 수행하고 프로그램 종료!!!!
}catch (IOException e) {
// TODO: handle exception
System.out.println("입출력오류");
}catch (Exception e) {                        
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}finally{
System.out.println("오류발생 유무와 관계없이 무조건 수행");
try{file.close();} catch (IOException e) {}
}
}
}//IOException수행후 ->nullpointException 체크
**/






import java.io.*;

class IOExceptionError {
public static void main(String args[]) {
FileReader file=null; //지역변수니까 초기화
try{
file = new FileReader("a.txt");   
// 만일 a.txt 파일이 없다면?
int i;
while((i = file.read()) != -1) {             // IOException
System.out.print((char)i);
}
}catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("해당파일이없음");        // -> 여기로와서 수행하고 프로그램 종료!!!!
}catch (IOException e) {
// TODO: handle exception
System.out.println("입출력오류");
}catch (Exception e) {                        
// TODO: handle exception !!!
System.out.println("모든 오류처리");
}finally{
System.out.println("오류발생 유무와 관계없이 무조건 수행");
try{file.close();} catch (Exception e) {}
}
}
}//Exception:오류처리를 위한,.






친절한 찰쓰씨
글쓴이
친절한 찰쓰씨
친절한 찰쓰씨 · 일상 UX 디자이너
기획·디자인·단상을 조용히 기록합니다.
작가 페이지에서 더 보기

이어서 읽기

새로워지기

꾸준히, 오래, 지치지 않고

Mar 31, 2026·8
새로워지기

테크 라이프 발란스

Feb 7, 2026·3
새로워지기

휴탈리티 박정렬

Feb 7, 2026·11