15
Jong Hyuk Park 17-1. 보조자료 예외처리 기존의 예외처리 방식 C++ 예외처리

17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

Jong Hyuk Park

17-1. 보조자료 예외처리

기존의 예외처리 방식

C++ 예외처리

Page 2: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

예외처리 소개

예외처리(exception handling)

프로그램의 비정상적인 상황 발생 시 처리하는 과정

대개의 경우 예외처리 하지 않는 경우 실행에러 발생

C++ Programming 2

Page 3: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

예외처리 없는 코드 예

C++ Programming 3

#include <iostream> using std::endl; using std::cout; using std::cin; int main(void) { int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b; cout<<"a/b의 몫 : "<<a/b<<endl; cout<<"a/b의 나머지 : "<<a%b<<endl; return 0; }

실행결과 1

두개의 숫자 입력 : 5 2

a/b의 몫 : 2

a/b의 나머지 : 1

실행결과 2

두개의 숫자 입력 : 5 0

<- !!! 실행에러 발생

Page 4: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

전통적 스타일 예외처리 코드 예

C++ Programming 4

#include <iostream> using std::endl; using std::cout; using std::cin; int main(void) { int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b; if(b==0){ cout<<"입력오류! 다시 실행 하세

요."<<endl; } else { cout<<"a/b의 몫 : "<<a/b<<endl; cout<<"a/b의 나머지 : "<<a%b<<endl; } return 0; }

실행결과 1

두개의 숫자 입력 : 5 2

a/b의 몫 : 2

a/b의 나머지 : 1

실행결과 2

두개의 숫자 입력 : 5 0

입력오류! 다시 실행 하세요.

Page 5: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

Jong Hyuk Park

C++ 예외처리

Page 6: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 (1)

try & catch

throw

C++ Programming 6

try {

/* 예외 발생 예상 지역 */

}

catch(처리 되어야 할 예외의 종류) {

/* 예외를 처리하는 코드가 존재할 위치 */

}

throw ex; // ex를 가리켜 보통은 그냥 “예외”라고 표현을 한다.

// 예외상황 발생 통보 시 사용

Page 7: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 (2)

C++ Programming 7

Page 8: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 코드 예 1

C++ Programming 8

#include <iostream> using std::endl; using std::cout; using std::cin; int main(void) { int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b; try{ if(b==0) throw b; cout<<"a/b의 몫 : "<<a/b<<endl; cout<<"a/b의 나머지 : "<<a%b<<endl; } catch(int exception){ cout<<exception<<" 입력."<<endl; cout<<"입력오류! 다시 실행 하세요."<<endl; } return 0; }

실행결과 1

두개의 숫자 입력 : 5 2

a/b의 몫 : 2

a/b의 나머지 : 1

실행결과 2

두개의 숫자 입력 : 5 0

0 입력.

입력오류! 다시 실행 하세요.

Page 9: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 코드 예 2

C++ Programming 9

#include <iostream> using std::endl; using std::cout; using std::cin; int divide(int a, int b); // a/b의 몫만 반환 int main(void) { int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b; try{ cout<<"a/b의 몫 : "<<divide(a, b)<<endl; } catch(int exception){ cout<<exception<<" 입력."<<endl; cout<<"입력오류! 다시 실행 하세요."<<endl; } return 0; } int divide(int a, int b) { if(b==0) throw b; return a/b; }

두개의 숫자 입력 : 5 0

0 입력.

입력오류! 다시 실행 하세요.

Page 10: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

스택 풀기 (Stack Unwinding)

C++ Programming 10

Page 11: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 코드 예

예외가 처리되지 않으면

• stdlib.h에 선언되어 있는 abort 함수의 호출에 의해 프로그램 종료

전달되는 예외 명시

• 그 이외의 예외가 전달되면 abort 함수의 호출

C++ Programming 11

int fct(double d) throw (int, double, char *)

{

. . . . .

}

Page 12: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 코드 예 3

C++ Programming 12

#include <iostream> using std::endl; using std::cout; using std::cin; int divide(int a, int b); // a/b의 몫만 반환 int main(void) { int a, b; cout<<"두개의 숫자 입력 : "; cin>>a>>b; try{ cout<<"a/b의 몫 : "<<divide(a, b)<<endl; } catch(char exception){ cout<<exception<<" 입력."<<endl; cout<<"입력오류! 다시 실행 하세요."<<endl; } return 0; } int divide(int a, int b) { if(b==0) throw b; // !! 에러, int 형 b 전달 return a/b; }

Page 13: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

C++ 예외처리 코드 예 4

C++ Programming 13

#include <iostream> using std::endl; using std::cout; using std::cin; int main(void) { int num; cout<<"input number: "; cin>>num; try{ if(num>0) throw 10; // int형 예외 전달. else throw 'm'; // char형 예외 전달. } catch(int exp){ cout<<"int형 예외 발생"<<endl; } catch(char exp){ cout<<"char형 예외 발생"<<endl; } return 0; }

실행결과 1

input number: 10

int형 예외 발생

실행결과 2

input number: -10

char형 예외 발생

Page 14: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

예외처리 클래스 예

C++ Programming 14

#include <iostream> using std::endl; using std::cout; using std::cin; char* account="1234-5678"; //계좌번호 int sid=1122; //비밀번호 int balance=1000; //잔액. class AccountExpt { char acc[10]; int sid; public: AccountExpt(char* str, int id){ strcpy(acc, str); sid=id; } void What(){ cout<<"계좌번호: "<<acc<<endl; cout<<"비밀번호: "<<sid<<endl; } };

int main(void) { char acc[10]; int id; int money; try{ cout<<"계좌번호 입력: "; cin>>acc; cout<<"비밀번호 4자리 입력: "; cin>>id; if(strcmp(account, acc) || sid!=id) throw AccountExpt(acc, id); cout<<"출금액 입력: "; cin>>money; if(balance<money) throw money; balance-=money; cout<<"잔액: "<<balance<<endl; } catch(int money){ cout<<"부족 금액: "<<money-balance<<endl; } catch(AccountExpt& expt){ cout<<"다음 입력을 다시 한번 확인 하세요"<<endl; expt.What(); } return 0; }

Page 15: 17-1. 보조자료 예외처리parkjonghyuk.net/lecture/2011-2nd-lecture/programming2/...예외처리 소개 예외처리(exception handling) 프로그램의 비정상적인 상황

Jong Hyuk Park