C++ Programming

Similar documents
C++ Programming

윤성우의 열혈 TCP/IP 소켓 프로그래밍

C++ Programming

C++ Programming

Microsoft PowerPoint - CSharp-10-예외처리

3장. Hello World

Microsoft PowerPoint - additional-11_13l.ppt [호환 모드]

C++ Programming

PowerPoint Template

Microsoft PowerPoint - Chapter 6.ppt

C++ Programming

슬라이드 1

Microsoft PowerPoint - [2009] 02.pptx

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

C프로-3장c03逞풚

Microsoft PowerPoint - additional01.ppt [호환 모드]

Cluster management software

쉽게 풀어쓴 C 프로그래밍

JAVA PROGRAMMING 실습 09. 예외처리

쉽게 풀어쓴 C 프로그래밍

080629_CFP °ø°³¿ë.hwp

Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

Microsoft PowerPoint - additional06.ppt [호환 모드]

PowerPoint Template

PowerPoint Presentation

1. 객체의생성과대입 int 형변수 : 선언과동시에초기화하는방법 (C++) int a = 3; int a(3); // 기본타입역시클래스와같이처리가능 객체의생성 ( 복습 ) class CPoint private : int x, y; public : CPoint(int a

슬라이드 1

Microsoft PowerPoint - 8ÀÏ°_Æ÷ÀÎÅÍ.ppt

Microsoft PowerPoint - additional07.ppt [호환 모드]

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - additional08.ppt [호환 모드]

1. auto_ptr 다음프로그램의문제점은무엇인가? void func(void) int *p = new int; cout << " 양수입력 : "; cin >> *p; if (*p <= 0) cout << " 양수를입력해야합니다 " << endl; return; 동적할

Semantic Consistency in Information Exchange

Microsoft PowerPoint - 03_(C_Programming)_(Korean)_Pointers

PowerPoint Presentation

class Sale void makelineitem(productspecification* spec, int qty) SalesLineItem* sl = new SalesLineItem(spec, qty); ; 2. 아래의액티비티다이어그램을보고 Java 또는 C ++,

PowerPoint Template

예외 예외정의예외발생예외처리예외전파 단정 단정의선언 단정조건검사옵션 2

슬라이드 1

설계란 무엇인가?

<443A5C4C C4B48555C B3E25C32C7D0B1E25CBCB3B0E8C7C1B7CEC1A7C6AE425CC0E7B0EDB0FCB8AE5C53746F636B5F4D616E D656E74732E637070>

Microsoft PowerPoint - C++ 5 .pptx

슬라이드 1

Microsoft PowerPoint - Chapter 1-rev

9장.예외와 단정

PowerPoint Template

C O N T E N T S 목 차 요약 / 1 I. 중남미화장품시장현황 / 3 Ⅱ. 주요국별시장정보 / 9 ( 트렌드 유통망 인증 ) 1. 브라질 / 9 2. 멕시코 / 콜롬비아 / 칠레 / 64 Ⅲ. 우리기업진출전략 / 79 # 첨부. 화장품관

PowerPoint Presentation

(Microsoft Word - \301\337\260\243\260\355\273\347.docx)

02 C h a p t e r Java

PowerPoint 프레젠테이션

PowerPoint 프레젠테이션

C 언어 프로그래밊 과제 풀이

Microsoft PowerPoint - 08_(C_Programming)_(Korean)_Preprocessing

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

Microsoft PowerPoint - additional03.ppt [호환 모드]

Microsoft PowerPoint - 9ÀÏ°_ÂüÁ¶ÀÚ.ppt

JAVA PROGRAMMING 실습 08.다형성

No Slide Title

JUNIT 실습및발표

PowerPoint Presentation

설계란 무엇인가?

설계란 무엇인가?

Design Issues

쉽게 풀어쓴 C 프로그래밊

No

(Microsoft PowerPoint - 07\300\345.ppt [\310\243\310\257 \270\360\265\345])

제목

쉽게 풀어쓴 C 프로그래밍

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

(Microsoft PowerPoint - 11\300\345.ppt [\310\243\310\257 \270\360\265\345])

pdf

Microsoft PowerPoint - 04-UDP Programming.ppt

Microsoft PowerPoint - chap02-C프로그램시작하기.pptx

3. 1 포인터란 3. 2 포인터변수의선언과사용 3. 3 다차원포인터변수의선언과사용 3. 4 주소의가감산 3. 5 함수포인터

Blog

歯JavaExceptionHandling.PDF

제 1 장 기본 개념

chap10.PDF

윤성우의 열혈 TCP/IP 소켓 프로그래밍

슬라이드 1

Microsoft PowerPoint - 07_(C_Programming)_(Korean)_Composite_Data_Types

BACK TO THE BASIC C++ 버그 헌팅: 버그를 예방하는 11가지 코딩 습관

윤성우의 열혈 TCP/IP 소켓 프로그래밍

The C++ Programming Language 5 장포인터, 배열, 구조체 5.9 연습문제 다음의선언문을순서대로작성해보자. 문자에대한포인터, 10개정수의배열, 10개정수의배열의참조자, 문자열의배열에대한포인터, 문자에대한포인터에대한포인터, 상수정수, 상수


유니티 변수-함수.key

Microsoft PowerPoint - 06_(C_Programming)_(Korean)_Characters_Strings

*금안 도비라및목차1~9

신림프로그래머_클린코드.key

Programming hwp

Network Programming

03장.스택.key

The C++ Programming Language 4 장타입과선언 4.11 연습문제 Hello,world! 프로그램을실행시킨다. 프로그램이컴파일되지않으면 B3.1 을참고하자. #include<iostream> //#include 문, 헤더파일, 전처리지시

歯박지원-구운몽.PDF

*금안14(10)01-도비라및목차1~12

PowerPoint Presentation

C O N T E N T 목 차 요약 / 4 Ⅰ. 서론 Ⅱ. 주요국별대형유통망현황 / Ⅲ. 시사점및진출방안 ( 첨부 ) 국가별주요수입업체

금오공대 컴퓨터공학전공 강의자료

Microsoft PowerPoint - chap06-2pointer.ppt

Transcription:

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