C++ Programming 예외처리 Seo, Doo-okok clickseo@gmail.com http://www.clickseo.com
목 차 예외처리 2
예외처리 예외처리 C++ 의예외처리 예외클래스와객체 3
예외처리 예외를처리하지않는프로그램 int main() int a, b; cout << " 두개의정수입력 :"; cin >> a >> b; cout << "a / b = " << a / b << endl; cout << "a %b=" << a % b << endl; return 0; 4
예외처리 (cont d) 전통적인스타일의예외처리 int main() 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; 5
C++ 의예외처리 기본적인예외처리메커니즘 try ~ catch throw try : 예외발생에대한검사범위를설정할때사용 catch : 예외를처리하는코드블록을선언할때사용 예외상황이발생하였음을알릴때사용 try if( 예외상황발생 ) catch(exception ex ) throw ex; 6
C++ 의예외처리 (cont d) 프로그램예제 : C++ 의예외처리메커니즘 #include <iostream> using std::cin; using std::cout; using std::endl; int main() 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; 7
C++ 의예외처리 (cont d) 스택풀기 (Stack Unwinding) 함수내부에서예외가발생하였을경우예외가전달되는과정이 함수의스택이풀리는순서와일치한다. try cout << "a / b = " << Divide(a, b) << endl; catch(int exception) int Divide(int a, int b) cout << " 입력오류 : " << exception << endl; cout << " 다시실행하세요!!!" << endl; if(b == 0) throw b; return a / b; 8
C++ 의예외처리 (cont d) 프로그램예제 : 스택풀기 (1/2) #include <iostream> using std::cin; using std::cout; t using std::endl; int Divide(int, int); int main() int a, b; cout << " 두개의정수입력 : "; cin >> a >> b; 9
C++ 의예외처리 (cont d) 프로그램예제 : 스택풀기 (2/2) 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; 10
C++ 의예외처리 (cont d) 처리되지않은예외 abort 함수호출 : stdlib.h int main() int a, b; int Divide(int a, int b) if(b == 0) throw b; cout << " 두개의정수입력 : "; cin >> a >> b; return a /b b; cout << "a / b = " << Divide(a, id b) << endl; return 0; 11
C++ 의예외처리 (cont d) 프로그램예제 : 처리되지않는예외 -- abort (1/2) #include <iostream> using std::cin; using std::cout; t using std::endl; int Divide(int, int); int main() int a, b; cout << " 두개의정수입력 : "; cin >> a >> b; 12
C++ 의예외처리 (cont d) 프로그램예제 : 처리되지않는예외 -- abort (2/2) try cout << "a / b = " << Divide(a, b) << endl; catch(char exception) // char 형예외처리!!! cout << " 입력오류 : " << exception << endl; cout << " 다시실행하세요!!!" << endl; return 0; int Divide(int a, int b) if(b == 0) throw b; // int 형예외발생 return a / b; 13
전달되는예외명시 C++ 의예외처리 (cont d) // function 함수는 int형예외를전달할수있다. int fuction(double d) throw (int) // function 함수는 int 형, double 형, char * 형예외가전달될수있다. int fuction(double d) throw (int, double, char *) // function 함수는어떠한예외도전달하지않는다 ( 예외전달시 abort 함수호출 ). int fuction(double d) throw ( ) 14
C++ 의예외처리 (cont d) 프로그램예제 : 하나의 try 블록과여러개의 catch 블록 #include <iostream> using std::cin; using std::cout; using std::endl; int main() int num; cout << " 정수입력 : "; cin >> num; try if(num > 0) throw 1; else throw 'e'; e; catch(int exception) cout << "int형예외발생 : " << exception << endl; catch(char exception) cout << "char형예외발생 : " << exception << endl; return 0; 15
예외클래스와객체 class User string string public: ; 예외클래스와객체 예외를발생시키기위해서클래스를정의하고객체를생성 객체를이용하면예외상황이발생한원인에대한정보를보다자세히담을수 있다. id; pass; try if(id!= user_id PASS!= user_pass) throw User(user _ id, user_pass); User(string id, string pass) this->id = id; catch(user &exception) this->pass = pass; exception.showuser(); void ShowUser() cout << " 아이디 : " << this->id << endl; cout << " 패스워드 : " << this->pass << endl; 16
예외클래스와객체 (cont d) 프로그램예제 : 예외클래스와객체 (1/2) #include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; string ID = "Clickseo"; string PASS = "1234"; class User string id; string pass; public: User(string id, string pass) ; void this->id = id; this->pass = pass; ShowUser() cout << " 아이디 : " << this->id << endl; cout << " 패스워드 : " << this->pass << endl; 17
예외클래스와객체 (cont d) 프로그램예제 : 예외클래스와객체 (2/2) int main() string user_id; string user_pass; try cout << " 아이디 : "; cin >> user_id; cout << " 패스워드 : "; cin >> user_pass; if(id!= user_id PASS!= user_pass) throw User(user_id, user_pass); cout << ID << " 님환영합니다!!!" << endl; catch(user &exception) cout << "\n 다음입력을다시한번확인하세요!!!" << endl; exception.showuser(); return 0; 18
예외클래스와객체 (cont d) 예외클래스와상속 예외의형태가유사한경우예외클래스를상속시키기도한다. try // 예외발생!!! catch(exception_a ex) catch(exception_b ex) catch(exception_c ception ex) // 1차비교 // 2 차비교 // 3 차비교 19
예외클래스와객체 (cont d) 프로그램예제 : 상속관계에있는예외객체전달 (1/3) #include <iostream> using std::cin; using std::cout; using std::endl; class Exception_A public: void ExceptionShow() cout << "Exception_A 예외!!!" << endl; ; class Exception_B : public Exception_A public: void ExceptionShow() cout << "Exception_B 예외!!!" << endl; ; 20
예외클래스와객체 (cont d) 프로그램예제 : 상속관계에있는예외객체전달 (2/3) class Exception_C : public Exception_B public: ; void ExceptionShow() cout << "Exception_C 예외!!!" << endl; void ExceptionFunction(int num) if(num == 1) throw Exception_A(); else if(num == 2) throw Exception_B(); else throw Exception_C(); 21
예외클래스와객체 (cont d) 프로그램예제 : 상속관계에있는예외객체전달 (3/3) int main() int num; cout << " 임의의정수입력 : "; cin >> num; try ExceptionFunction(num); catch(exception_c ex) cout << "\ncatch(exception_c ex) 에의한처리..." << endl; ex.exceptionshow(); catch(exception_b ex) cout << "\ncatch(exception_b ex) 에의한처리..." << endl; ex.exceptionshow(); catch(exception_a ex) cout << "\ncatch(exception_a ex) 에의한처리..." << endl; ex.exceptionshow(); return 0; 22
참고문헌 [1] 윤성우, 열혈강의 C++ 프로그래밍, 프리렉, 2007. [2] 이현창, 뇌를자극하는 C++ 프로그래밍, 한빛미디어, 2008. [3] H.M. HM Deitel, P. J. Deitel, C++ HOW TO PROGRAM : 6th Edition, Prentice Hall, 2009. [4] Wikipedie, http://www.wikipedia.org/. 이강의자료는저작권법에따라보호받는저작물이므로무단전제와무단복제를금지하며, 내용의전부또는일부를이용하려면반드시저작권자의서면동의를받아야합니다. Copyright Clickseo.com. All rights reserved. 23