● try 블록에는 예외가 발생할 가능성이 있는 코드를 작성하고 try 블록 안에서 예외가 발생하는 경우 catch 블록이 수행됨
코드로 알아보기
package ch08;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class FileExceptionHanding {
public static void main(String[] args) {
// 파일 <-- IO<--!!!
FileInputStream fis = null;
// try {
// fis = new FileInputStream("temp");// <-- 예외가 발생할수있다라고 알려준다
// } catch (FileNotFoundException f) {
// System.out.println("temp 라는 파일이 없습니다");
// }
// try-catch-finally
try {
fis = new FileInputStream("test1.txt");
return;
} catch (FileNotFoundException e) {
// e.printStackTrace();
System.out.println("파일명 확인해주세용!! 힛");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 반드시 수행되는 코드
// 심지어 return 키워드를 만나더라도 실행이된다 !! 아주 강력한 녀석
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않음");
}// end of main
}// end class
package ch08;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class FileExceptionHanding2 {
public static void main(String[] args) {
Myfile file = new Myfile();
try {
file.inputData("하이~~");
} catch (FileNotFoundException e) {
System.out.println("파일이 없네요");
}
}// end of main
}// end class
class Myfile {
// throws 던진다-->
// 누군가가 내가만든 Myfile 를 사용 할때
// inputData 오류가 날수 있으니 예외처리 흐름은 사용 하는 사람이
// 알아서 구현해!!
public void inputData(String str) throws FileNotFoundException {
FileInputStream fis;
fis = new FileInputStream("test1.txt");
}
}
package ch08;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class MyException {
public static void main(String[] args) {
TxtFileInputManager inputManager = new TxtFileInputManager("test.txt");
try {
String result = inputManager.readTxrFileData();
System.out.println("result " + result);
} catch (IOException e) {
e.printStackTrace();
}
}// end main
}// end class
class TxtFileInputManager {
// 외부 파일을 내 메모리 상으로 가져올수 있는 녀석
private FileInputStream fis;
private String fileName;
public TxtFileInputManager(String fileName) {
this.fileName = fileName;
}
public String readTxrFileData() throws IOException {
// IOException 부모!
// FileNorFoundException 보다 부모
fis = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(fis);
// KEY=VALUE <-- 데이터를 읽어주는녀석
// name=홍길동 --> 홍길동 --> 추출
String name = prop.getProperty("name");
return name;
}
}
예제풀이
package ch08;
public class MainTest2 {
public static void main(String[] args) {
// 예외 처리 구문작성
// 10 /0 연산 시키고 예외 처리까지 작성해주세여
// try
// 클래스 설계
int ten = 10;
int zero = 0;
try {
int result = ten / zero;
// int result = zero/ten;
System.out.println("수식 결과는 : " + result);
} catch (Exception e) {
System.out.println("수식이 맞지 않습니다");
} finally {
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않았습니다");
}// end of main
}// end of class
package ch08;
public class MainTest2 {
public static void main(String[] args) {
// 예외 처리 구문작성
// 10 /0 연산 시키고 예외 처리까지 작성해주세여
// try
// 클래스 설계
int ten = 10;
int zero = 0;
try {
//int result = ten / zero;
int result = zero/ten;
System.out.println("수식 결과는 : " + result);
} catch (Exception e) {
System.out.println("수식이 맞지 않습니다");
} finally {
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않았습니다");
}// end of main
}// end of class
'자바' 카테고리의 다른 글
GUI 이벤트 리스너 (0) | 2023.02.21 |
---|---|
GUI 이미지 겹치기 (0) | 2023.02.21 |
예외 처리 (0) | 2023.02.21 |
GUI를 이용한 이미지 출력 연습(Swing) (1) | 2023.02.21 |
외부 클래스를 이용한 집만들기! (1) | 2023.02.21 |