문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch11 [2022/01/04 11:00] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch11 [2023/07/13 17:29] (현재) clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch11에서 프로그램:java:ㄴhomework:book_source:ch11(으)로 이동되었습니다 |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
+ | =====예외처리===== | ||
+ | |||
+ | **try ~catch문의 구조** | ||
+ | |||
+ | try { | ||
+ | // 예외가 발생할 가능성이 있는 문장 코드 | ||
+ | } catch (Exception1 e1) { | ||
+ | // Exception1이 발생할 경우, 실행될 문장 | ||
+ | } catch (Exception1 e2) { | ||
+ | // Exception2이 발생할 경우, 실행될 문장 | ||
+ | ... | ||
+ | } catch (ExceptionX eX){ | ||
+ | // ExceptionX가 발생할 경우, 실행될 문장 | ||
+ | } | ||
+ | | ||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx0 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | System.out.println(1); | ||
+ | System.out.println(2); | ||
+ | System.out.println(3/ | ||
+ | System.out.println(4); | ||
+ | System.out.println(5); | ||
+ | System.out.println(6); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | System.out.println(1); | ||
+ | try { | ||
+ | System.out.println(2); | ||
+ | System.out.println(3/ | ||
+ | System.out.println(4); | ||
+ | } catch(ArithmeticException e) { | ||
+ | System.out.println(5); | ||
+ | } | ||
+ | System.out.println(6); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ^<color # | ||
+ | | - 발생한 예외와 일치하는 | ||
+ | |- 만약 일치하는 catch문이 있다면, 해당 catch문의 블럭 내의 실행문을 실행하고 전체 try-catch 구문이 종료된다. 만약 일치하는 catch문이 없으면 예외 처리를 하지 못한다.| | ||
+ | ^<color # | ||
+ | | - catch 구문을 모두 확인하지 않고, 전체 try-catch 구문이 종료된다.| | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | System.out.println(1); | ||
+ | try { | ||
+ | System.out.println(2); | ||
+ | System.out.println(3); | ||
+ | System.out.println(4); | ||
+ | } catch(ArithmeticException e) { | ||
+ | System.out.println(5); | ||
+ | } | ||
+ | System.out.println(6); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | : | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx4 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | int[] arr = {1,2,3}; | ||
+ | System.out.println(arr[2]); | ||
+ | System.out.println(3/ | ||
+ | Integer.parseInt(" | ||
+ | } catch(ArithmeticException e) { | ||
+ | System.out.println(" | ||
+ | } catch (ArrayIndexOutOfBoundsException e) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ^<color # | ||
+ | |자식 Exception > Exception 순으로 배치| | ||
+ | |||
+ | : | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | System.out.println(" | ||
+ | try { | ||
+ | System.out.println(" | ||
+ | System.out.println(3/ | ||
+ | } catch(Exception e) { | ||
+ | System.out.println(" | ||
+ | } finally { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | ===== 예외 강제 발생 ===== | ||
+ | |||
+ | ^<color # | ||
+ | |throw new Exception( " 예외 발생 ");| | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx5 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | System.out.println(" | ||
+ | try { | ||
+ | throw new Exception(" | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====예외 떠넘기기===== | ||
+ | |||
+ | **메서드에서 예외 선언** | ||
+ | void 메서드명() throws Exception1, Exception2... { | ||
+ | ... | ||
+ | } | ||
+ | |||
+ | <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 java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class ExceptionEx7 { | ||
+ | |||
+ | public static void main(String[] args) throws Exception { | ||
+ | try { | ||
+ | first(); | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(" | ||
+ | System.out.println(e.getMessage()); | ||
+ | throw e; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | static void first() throws Exception { | ||
+ | try { | ||
+ | second(); | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(" | ||
+ | throw e; // 예외 재발생 | ||
+ | } | ||
+ | } | ||
+ | |||
+ | static void second() throws Exception { | ||
+ | try { | ||
+ | throw new Exception(" | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(" | ||
+ | throw e; // 예외 재발생 | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | public static void main(String[] args) throws Exception { | ||
+ | try { | ||
+ | first(); | ||
+ | } | ||
+ | catch (Exception e) { | ||
+ | System.out.println(" | ||
+ | System.out.println(e.getMessage()); | ||
+ | throw e; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ===== 사용자 정의 예외 클래스 ===== | ||
+ | |||
+ | class 클래스명 extends Exception { | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | |||
+ | **LoginException.java** | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | public class LoginException extends Exception { | ||
+ | |||
+ | LoginException(String msg) { | ||
+ | super(msg); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | **ExceptionEx8.java** | ||
+ | |||
+ | <code java> | ||
+ | package chapter11; | ||
+ | |||
+ | import java.util.Scanner; | ||
+ | |||
+ | public class ExceptionEx8 { | ||
+ | |||
+ | static String user_id = " | ||
+ | static String user_pw = " | ||
+ | |||
+ | 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()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | : | ||
+ | |||
+ | **기존 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(); | ||
+ | } | ||
+ |