nme.kr

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
프로그램:java:ㄴhomework:book_source:ch11 [2022/01/05 00:14]
clayeryan@gmail.com [11.5 예외 떠넘기기]
프로그램:java:ㄴhomework:book_source:ch11 [2023/07/13 17:29] (현재)
clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch11에서 프로그램:java:ㄴhomework:book_source:ch11(으)로 이동되었습니다
줄 157: 줄 157:
  
 =====예외 떠넘기기===== =====예외 떠넘기기=====
 +
 +**메서드에서 예외 선언**
 +  void 메서드명() throws Exception1, Exception2... {
 +  ...
 +  }
  
 <code java> <code java>
 +package chapter11;
 +
 +public class ExceptionEx6 {
 +
 + public static void main(String[] args) {
 + try {
 + first();
 + } catch (Exception e) {
 + System.out.println(e.getMessage());
 + }
 + }
 +
 + static void first() throws Exception {
 + second();
 + }
 +
 + static void second() throws Exception {
 + throw new Exception("예외 발생");
 + }
 +
 +}
  
 </code> </code>
줄 165: 줄 191:
  
 <code java> <code java>
 +package chapter11;
 +
 +public class ExceptionEx7 {
 +
 + public static void main(String[] args) throws Exception {
 + try {
 + first();
 + } catch (Exception e) {
 + System.out.println("main() 예외 처리");
 + System.out.println(e.getMessage());
 + throw e;
 + }
 + }
 +
 + static void first() throws Exception {
 + try {
 + second();
 + } catch (Exception e) {
 + System.out.println("first() 예외 처리");
 + throw e; // 예외 재발생
 + }
 + }
 +
 + static void second() throws Exception {
 + try {
 + throw new Exception("예외 발생");
 + } catch (Exception e) {
 + System.out.println("second() 예외  처리");
 + throw e; // 예외 재발생
 + }
 + }
 +
 +}
  
 </code> </code>
-==== 11.6 사용자 정의 예외 클래스 ==== 
  
 +<code java>
 + public static void main(String[] args) throws Exception {
 + try {
 + first();
 + }
 + catch (Exception e) {
 + System.out.println("Main() 예외 처리");
 + System.out.println(e.getMessage());
 + throw e;
 + }
 +}
 +</code>
 +
 +
 +
 +
 +===== 사용자 정의 예외 클래스 =====
 +
 +  class 클래스명 extends Exception {
 +       클래스명 (String msg) {
 +             super(msg);
 +        }
 +   }
 + 
 +**LoginException.java**
 +
 +<code java>
 +package chapter11;
 +
 +public class LoginException extends Exception {
 +
 + LoginException(String msg) {
 + super(msg);
 + }
 +}
 +
 +</code>
 +
 +**ExceptionEx8.java**
 +
 +<code java>
 +package chapter11;
 +
 +import java.util.Scanner;
 +
 +public class ExceptionEx8 {
 +
 + static String user_id = "seo";
 + static String user_pw = "smg1234";
 +
 + public static void main(String[] args) throws Exception {
 +
 + try {
 + Scanner scan = new Scanner(System.in);
 + System.out.print("아이디 : ");
 + String input_id = scan.nextLine();
 +
 + System.out.print("비밀번호 : ");
 + String input_pw = scan.nextLine();
 +
 + if (!user_id.equals(input_id)) {
 + throw new LoginException("아이디가 올바르지 않습니다.");
 + } else if (!user_pw.equals(input_pw)) {
 + throw new LoginException("비밀번호가 올바르지 않습니다.");
 + } else {
 + System.out.println("로그인 성공");
 + }
 + } catch (Exception e) {
 + System.out.println(e.getMessage());
 + }
 +
 + }
 +
 +
 +}
 +</code>
 +
 +:wr:<color #ff7f27>**try-with-resource**</color>
 +
 +**기존 try~catch 방식**
 +
 +  FileInputStream is = null;
 +  BufferedInputStream bis = null;
 +  try {
 + is = new fileInputStream("파일명")
 + bis = new BufferdInputStream(is);
 + int data = -1;
 + while((data = bis.read()) !=-1){
 + System.out.print((char)data);
 + }
 +  } finally {
 + // close 메서드 호출
 + if ((is != null) is.close();
 + if (bis != null) bis.close();
 +  }
 +
 +
 +**try-with-resource 방식**
  
 +  try(
 +  FileInputStream is = new FileInputStream("파일명");
 +  BufferedInputStream bis = new BufferdInputStream(is)
 +  ) {
 +  int data = -1;
 +  while ((data = bis.read()) != -1) {
 + System.out.print((char) data);
 + }
 +  } catch (IOException e) {
 + e.printStackTrace();
 +  }
 +