1 / 22

예외처리

예외처리. 담당 : 안 유 정 교수 연구실 : 연구동 410 호 e-mail : youjahn@gmail.com. 차례. 예외의 정의 예외의 종류 - 시스템 정의 예외 - 프로그래머 정의 예외 예외발생 예제 예외 처리의 종류 - 시스템 정의 예외에 대한 처리 - 프로그래머 정의 예외에 대한 처리 예외 전파. 예외 (exceptions) 의 정의. 예외란 프로그램 실행 중에 발생하는 예기치 않은 사건 예외가 발생하는 예 정수를 0 으로 나누는 경우

brygid
Download Presentation

예외처리

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 예외처리 담당 : 안 유 정 교수 연구실 : 연구동 410호 e-mail : youjahn@gmail.com

  2. 차례 • 예외의 정의 • 예외의 종류 - 시스템 정의 예외 - 프로그래머 정의 예외 • 예외발생 예제 • 예외 처리의 종류 - 시스템 정의 예외에 대한 처리 - 프로그래머 정의 예외에 대한 처리 • 예외 전파

  3. 예외(exceptions)의 정의 • 예외란 프로그램 실행 중에 발생하는 예기치 않은 사건 • 예외가 발생하는 예 • 정수를 0으로 나누는 경우 x = 10 / 0; • 배열의 첨자가 음수 또는 범위를 벗어나는 경우 int a[] = new int [5]; → a[5] = 10; • 성적 처리하는 프로그램에서 데이터(점수)가 음수로 입력될 때

  4. 예외(exceptions)의 정의 • 자바에서 예외 클래스들의 계층구조 Object 예외에 관한 클래스들 중 최상위 클래스 Throwable 시스템 정의 예외 프로그래머 정의 예외 Error Exception RuntimeException LinkageError InstantiationException ArithmeticException

  5. 예외(exceptions)의 정의 • 자바에서는 예외 하나를 클래스로 정의하고 있다. 즉, 예외를 객체로 취급한다. • Throwable클래스 • 자바에서 예외에 관한 클래스들 중 최상위 클래스 • 자바에서 예외의 종류 • 시스템 정의 예외 – 자바 시스템에 이미 만들어져 있는 예외 • 프로그래머 정의 예외 – 프로그래머가 만들어 사용하는 예외

  6. 예외의 종류 –시스템정의 예외 • 시스템 정의 예외 • 자바에 이미 정의되어있는 예외 • 시스템정의 예외 발생시 자바 시스템에 의해 자동으로 예외 처리 • Error클래스와 RunTimeException 클래스의 하위 클래스들로 정의됨 • 대표적인 시스템 정의 예외 클래스 • Error 클래스 하드웨어 관련 예외를 정의하고 있음 예) 하드디스크 포멧이 망가진 경우, 메모리 파괴, 스택 오버플로우 등 • RunTimeException 클래스 프로그래머의 실수로 인해 더 이상 프로그램의 실행을 지속할 수 없을 때 발생하는 실행시간 오류 예) 0으로 나누기 등의 산술적 예외, 배열의 크기가 음수로 지정된 경우

  7. 시스템 정의 예외의 종류 • RuntimeException 클래스의 하위 클래스들

  8. 예외의 종류 –프로그래머정의 예외 • 프로그래머 정의 예외 • 프로그래머가 필요에 의해 정의한 예외 • 프로그래머에 의해 의도적으로 발생된다. • 예외 정의, 예외 발생, 예외 처리를 모두 프로그래머가 꼭 해야 한다. • Exception 클래스의 하위 클래스들로 정의됨 • 예 : class UserException extends Exception { }

  9. 예외 발생 예제 • 시스템 정의 예외 예제 • 시스템 정의 예외는 자바에 이미 정의되어 있으므로 • 예외 상황이 생겼을때 자동으로 예외가 발생된다. • 예) 숫자를 0으로 나누는 경우의 시스템 정의 예외 DividebyZero.java  public class DividebyZero {   public static void main(String[] args) {      int x=10, y=0;      System.out.println(x+"/"+y+"="+x/y);   }  } ArithmeticException 이 자동으로 발생

  10. 예외 발생 예제 • 시스템 정의 예외 발생 예제(결과) - 숫자를 0으로 나누는 경우의 시스템 정의 예외 발생 결과

  11. 예외 발생 예제 • 시스템 정의 예외 발생 예제 - 예) 배열의 첨자가 범위를 벗어날 때 발생하는 시스템 예외 ArrayIndexOutOfBounds Exception 자동으로 발생 IndexOutExcept.java public class IndexOutExcept {   public static void main(String[] args) {      int a[] = new int[5];      for (int i=0; i<=5; i++) {         // i=5일 때 예외 발생 a[i] = i;                              System.out.print(" a["+i+"]="+a[i]);      }      System.out.println();   }  }

  12. 예외 발생 예제 • 시스템 정의 예외 발생 예제(결과) - 배열의 첨자가 범위를 벗어날 때 발생하는 시스템 예외 결과

  13. 예외 발생 예제 • 프로그래머 정의 예외 발생 예제 - 입력데이터가 없는 경우에 대한 예외 정의, 발생, 처리 예 UserDefinedExcept.java class EmptyData extends Exception { } public class UserDefinedExcept {    public static void main(String[] args) { try {       if (args.length == 0) throw new EmptyData(); System.out.println(agrs[0]); }catch (EmptyData e) { System.out.println(“데이터가 입력되지 않았어요!"); }     } } 프로그래머 예외 정의하기 프로그래머 정의 예외 발생 프로그래머 정의 예외 처리

  14. 예외 발생 예제 • 프로그래머 정의 예외 발생 예제(결과)

  15. 예외 처리 • 시스템 정의 예외 시스템에서 자동으로 예외처리해 줌 프로그래머가 별도의 예외처리를 해주어도 됨 • 프로그래머 정의 예외 반드시 프로그래머가 예외발생 및 처리를 해줘야 됨

  16. 예외 처리 • 예외처리 형식 try { 일반 프로그램 문장들 } catch(예외 클래스1 객체명1) { 예외1에 대한 예외처리 루틴 } catch(예외 클래스2 객체명2) { 예외2에 대한 예외처리 루틴 }      …… [ finally { 예외 발생과 상관없이 무조건 실행해야 하는 문장 } ]

  17. 시스템 정의 예외 - 예외 처리 예제 • 시스템 정의 예외를 프로그래머가 예외처리 한 예제 - 숫자를 0으로 나누는 경우 DividebyZero2.java  public class DividebyZero2 { public static void main(String[] args) { int x=10, y=0; try { System.out.println(x+"/"+y+"="+x/y); } catch (ArithmeticException e) { System.out.println("0으로 나누기 금지“); } } }

  18. 시스템 정의 예외 - 예외 처리 예제 • 시스템 정의 예외를 프로그래머가 예외처리 한 예제 - 숫자를 0으로 나누는 경우

  19. 시스템 정의 예외 - 예외 처리 예제 • 시스템 정의 예외를 프로그래머가 예외처리 해보자. 배열의 첨자가 범위를 벗어나는 시스템 정의 예외인 ArrayIndexOutOfBoundsException 예외를 프로그래머가 예외 처리하도록 다시 프로그램 해보자. IndexOutExcept.java public class IndexOutExcept {   public static void main(String[] args) {      int a[] = new int[5];      for (int i=0; i<=5; i++) {         // i=5일 때 예외 발생 a[i] = i;                              System.out.print(" a["+i+"]="+a[i]);      }      System.out.println();   }  }

  20. 시스템 정의 예외 - 예외 처리 예제 • 시스템 정의 예외를 프로그래머가 예외처리 해보자. IndexOutExcept.java public class IndexOutExcept {   public static void main(String[] args) { try {      int a[] = new int[5];       for (int i=0; i<=5; i++) {         // i=5일 때 예외 발생 a[i] = i;                              System.out.print(" a["+i+"]="+a[i]);       }       System.out.println(); } catch (ArrayIndexOutOfBoundsException e) { System.out.print(“배열의 크기를 벗어났어요!”); } }}

  21. 프로그래머 정의 예외 - 예외 처리 예제 • 프로그래머 정의 예외에 대한 예외 처리 프로그래머 정의 예외인 MinusException 예외를 만들어서, 정수를 입력받 아 입력된 숫자가 음수이면 MinusException 예외를 발생시키고 예외처리 하는 프로그램을 만들어보자. class MinusException extends Exception {}// 프로그래머 정의 예외 public class ThrowsClause { public static void main(String[] args) { int num; try { num=Integer.parseInt(args[0]); if (num<0) throw new MinusException(); // 음수 예외 발생 System.out.println("읽어들인 숫자는 "+num); } catch (MinusException e) { // 음수발생 예외에 대한 System.out.println("양수를 입력하시오!"); // 예외처리 } }}

  22. 프로그래머 정의 예외 - 예외 처리 예제 • 정수를 입력받아 읽어들인 수가 음수이면 예외 발생 예제 (결과)

More Related