6 11 2 PLI on condition condition default

  • Slides: 29
Download presentation

6 11. 2 PL/I의 예외 처리 • 시스템 제공 on condition(조건)만 사용 가능 •

6 11. 2 PL/I의 예외 처리 • 시스템 제공 on condition(조건)만 사용 가능 • 수치계산 condition : default enable • 1. CONVERSION 2. FIXEDOVERFLOW 4. UNDERFLOW 5. ZERODIVIDE Program test condition : default disable 6. SIZE 9. CHECK • 3. OVERFLOW 7. SUBSCRIPTRAGE 8. STRINGRANGE 10. AREAR 입출력 condition : always enable 11. ATTENSION 12. CONDITION 13. ENDFILE 14. ENDPAGE 15. ERROR 16. FINISH 17. KEY 18. NAME 19. PENDING 20. RECORD 21. TRANSMIT 22. UNDEFINITEFILE

9 11. 2 PL/I의 예외 처리 1 2 3 4 5 6 7 8

9 11. 2 PL/I의 예외 처리 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 TEST:PROCEDURE OPTIONS(MAIN); DECLARE (PERSON , GRADE) FIXED; DECLARE LAST FIXED INIT(0); DECLARE SUMMARY(1: 50 , 0: 100) FIXED; ON ENDPAGE(SYSPRINT) /* 오류 메시지 리스트를 위한 제목 인쇄 */ BEGIN PUTPAGE; PUT LIST(' ' , 'LIST ERROR DATA'); END; ON SUBSCRIPTRANGE BEGIN IF LAST = 0 THEN SIGNAL ENDPAGE(SYSPRINT); /* 오류가 첫 번째로 발견되면 오류 리스트의 제목 인쇄 */ IF I NOT = LAST THEN /* 새로운 경우를 검사 */ PUT SKIP DATA(I , PERSON , GRADE); LAST = I END; ON ENDFILE(SYSIN) BEGIN; PUT PAGE LIST(' ' , `OUTPUT FOR CLASS'); PUT SKIP LIST(' ' , 'SUMMARY'); PUT SKIP(3) LIST(SUMMARY); IF LAST NOT = 0 THEN PUT SKIP(4) LIST ('SOME DATA IS INCOMPLETE, SEE OUTPUT'); END; SUMMARY = 0; A:DO I = 1 BY 1; GET LIST(PERSON , GRADE); SUMMARY(PERSON , GRADE) = SUMMARY(PERSON , GRADE) + GRADE; END A; END TEST;

15 11. 3 Ada의 예외 처리 < Ada 예외 처리 예 > 1 procedure

15 11. 3 Ada의 예외 처리 < Ada 예외 처리 예 > 1 procedure P is 2 BAD-FORMAT:exception; 3 procedure Q is 4 begin 5. . . 6 if S /='' then raise BAD_FORMAT;end if; 7. . . 8 end Q; 9 procedure R is 10 begin 11 Q; 12 exception when BAD_FORMAT => -- handler body 1 13 end R; 14 begin 15 R; 16 Q; 17 exception when BAD_FORMAT => -- handler body 2 18 end P;

17 11. 3 Ada의 예외 처리 < Ada 예외 전파 사용 예 > 1

17 11. 3 Ada의 예외 처리 < Ada 예외 전파 사용 예 > 1 2 3 4 5 6 procedure A is procedure B is FLOPPED :exception; begin -- sequence of statements exception when FLOPPED => raise end; begin B; exception when others => -- handle FLOPPED if possible else raise; end;

18 11. 3 Ada의 예외 처리 1 2 3 4 5 6 7 8

18 11. 3 Ada의 예외 처리 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 procedure DOSOMETHING is HANDLE_ERROR:exception; begin -- perform some set of actions exception when HANDLE_ERROR => -- error handing code end; end DOSOMETHING; function SECURE_DIVIDE(X , Y:REAL) return REAL is begin return X / Y; exception when NUMERIC_ERROR => return 0; end SECURE_DIVIDE; procedure COPYOVER is begin OPEN(INFILE); OPEN(OUTFILE); loop GET(INFILE , CHARACTER); PUT(OUTFILE , CHARACTER); end loop; exception when END_OF_FILE => PUT(OUTFILE , EOF); CLOSE(OUTFILE); CLOSE(INFILE); end COPYOVER; < Ada에서의 다양한 예외들의 사용 예 >

 • • • • • • • • • #include <iostream. h> void

• • • • • • • • • #include <iostream. h> void main() { //* 어떤 예외도 발생 가능 int new_grad, index, limit_1, limit_2, freq [10]; short int eof_condition; try { while (1) { If (!cin >> new_grade) //* cin이 eof를 만나면 throw eof_condition; //* eof_condition 예외 발생 index = new_grade /10; try {{ {{ try if (index < 0 ||ifindex > 9) (index < 0 || index > 9) throw (new_grade) ; freq [index]++; } //* 속 try 복합문 끝 } //* 속 try catch (int grade) { //* 오류{처리기 catch (int첨자 grade) //* 첨자 오류 처리기 if (grade == 100) freq [9]++; else cout << “ Errorcout -- new “ << grade << “grade Error : -new grade : “ << grade << “ is out of range “ << end 1; } //* catch(int grade) 끝 } //* 속 try 복합문과 catch문 쌍끝 } //* while 문 끝 } //* 밖 try 복합문 끝 catch (short int) { //* eof 예외 처리기 cout << “ Limits Frequency ” << end 1; for ( index = 0 ; index <10 ; index++) { limit_1 = 10 * index; limit_2 = limit_1 + 9; if (index == 9) limit_2 = 100; cout << limit_1 << limit_2 << freq [index] << end 1; } //* for 문 끝 } //* catch(short int) 끝 } //* main 끝 21

22 11. 4 Java의 예외 처리 • 예외의 종류 – 시스템 정의 예외(System-defined exception

22 11. 4 Java의 예외 처리 • 예외의 종류 – 시스템 정의 예외(System-defined exception or predefined exception) • 프로그램의 부당한 실행에 의한 예외 • 비검사 예외(unchecked exception) • Error와 Runtime. Exception 클래스 – Arithmetic. Exception, IOException, Index. Out. Of. Bounds. Exception, Array. Store. Exception, Negative. Array. Size. Exception, Null. Pointer. Exception, Security. Exception, Illegal. Monotor. Exception등… – 프로그래머 정의 예외(Programmer-defined exception) • 프로그래머에 의도적으로 야기되는 예외 • 검사 예외(checked exception) : 예외 처리기의 존재 검사

23 11. 4 Java의 예외 처리 < Throwable 클래스의 계층 구조 > Object Throwable

23 11. 4 Java의 예외 처리 < Throwable 클래스의 계층 구조 > Object Throwable Error Linkaqe. Error Thread. Death Exception Runtime. Exception Checked. Exception Virtual. Machine. Error Arithemtic. Exception

24 11. 4 Java의 예외 처리 • 자바 예외 처리기 – C++ 예외 처리기와

24 11. 4 Java의 예외 처리 • 자바 예외 처리기 – C++ 예외 처리기와 동일한 형태 – catch문은 매개변수를 갖는다 – 클래스는 미리 정의한 클래스 Throwable의 후손이어야 함 – – – 예외 정의 • 예외 발생 class My. Exception extends Exception { public My. Exception( ) { } public My. Exception(String message) { super(message); } } My. Exception my. Exception. Object = new My. Exeption( ); – … – throw my. Exception. Object; – 또는 – throw new My. Exception(“Here is Hong. Kil. Dong”);

Import java. io. *; Class Negativeinput. Exception extends Exception { //** 자료 끝을 다루는

Import java. io. *; Class Negativeinput. Exception extends Exception { //** 자료 끝을 다루는 예의 정의 public Negative. Input. Exception () { System. out. println(“End of input data reached”); } //** 생성자 끝 } //** Negative. Input. Exception 클래스 끝 Class Grad. Dist { int new. Grade, index, limit_1, limit_2; int [] freq = {0, 0, 0, 0}; void build. Dist () throws IOException { Data. Input. Stream in = new Data. Input. Stream(System. in); Try { while (true) { systme. out. println(“Please input a grade”); new. Grade = Integer. parse. Int(in. read. Line()); if (new. Grade < 0) throw new Negative. Input. Exception(); index = new. Grade /10; try { freq[index]++; } //** 안쪽 try절 끝 catch (Array. Index. Out. Of. Bounds. Exception) { if (new. Grade ==100) freq[9]++; else System. out. println(“Error – new grade : “ + new. Grade + “ is out of range”); } //** catch(Array. Index. . )절 끝 } //** while(true). . 문 끝 } //** 바깥쪽 try절 끝 Catch (Negative. Input. Exception) { System. out. println (“n. Limits Frequencyn”); for (index=0; index<10; index++) { limit_1 = 10 * index; limit_2 = Limit_1 + 9; if (index ==9) limit_2=100; System. out. println(“ “+limit_1 + “-” + limit_2 + “ “ + freq[index]); } //** for 문 끝 (index = 0; . . } //** catch 문 끝 (Negative. Input. Exception. . } //** method build. Dist 끝 27