SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
3
try
{
if (/* Exception Condition */)
throw new std::exception("Error Description");
}
catch (std::exception e)
{
std::cout << "Exception : " << e.what() << std::endl;
}
4
try
{
if (/* Exception Condition */)
throw new std::exception("Error Description");
}
catch (std::exception e)
{
cout << "Exception : " << e.what() << endl;
}
예외가 발생할 수 있는 부분을 정의
try { } 와 catch { } 는 한쌍
예외를 발생시킴
try 안에서 발생한 예외 중 e를 catch
예외 처리
5
#include <iostream>
void main()
{
int a = 9;
int b = 0;
int c = a / b;
std::cout << c << std::endl;
}
void main()
{
int a = 9;
int b = 0;
int c = 0;
if (b != 0)
{
c = a / b;
std::cout << c << std::endl;
}
else
std::cout << "Exception : Divide by zero" << std::endl;
}
6
원래 Code와 예외처리 부분이 뒤죽박죽
void main()
{
int a = 9;
int b = 0;
int c = 0;
try
{
if (b == 0) throw b;
c = a / b;
std::cout << c << std::endl;
}
catch (int divided)
{
std::cout << "Exception : Divide by " << divided << std::endl;
}
}
7
좀 더 명확하게 구분가능
(이라고 주장하기엔 앞 Page 예제랑 별 차이가…)
void main()
{
int a = 9;
int b = 0;
int c = 0;
try
{
c = Divide<int>(a, b);
std::cout << c << std::endl;
}
catch (int divided)
{
std::cout << "Exception : Divide by " << divided << std::endl;
}
}
8
함수를 사용하면서 예외 처리 할려면…
template <typename T>
T Divide(T a, T b)
{
if (b == 0) throw std::exception("Divide by zero");
return a / b;
}
9
• void func(int a)
• void func(int a) throw(int);
• void func(int a) throw(char *, int);
• void func(int a) throw();
• void func(int a) noexcept;
모든 타입의 예외가 발생 가능하다.
int 타입 예외를 던질 수 있다.
타입이 2가지 이상일 경우는 , 로 나열
예외를 발생하지 않는다.
C++11 Style
 #include <exception>하면 대부분의 경우 사용 가능
11
12
#include <iostream>
#include <new>
void main()
{
char* ptr;
try
{
ptr = new char[(~unsigned int((int)0) / 2) - 1];
delete[] ptr;
}
catch (std::bad_alloc &ba)
{
std::cout << ba.what() << std::endl;
}
}
• bad_alloc: 메모리 할당 오류로서 new 연산에서 발생 <new>
• bad_cast : 형변환 오류로서 dynamic_cast 에서 발생 <typeinfo.h>
• bad_type_id : typeid에 대한 피 연산자가 널 포인터인 경우 발생
• bad_exception : 예기치 못한 예외로서 함수 발생 목록에 있지 않는 예외
• bad_logic_error : 클래스 논리 오류
• invalid_argument, length_error, out_of_range 의 기본 <stdexcept>
• runtime_error : 실행 오류로 overflow_error,
underflow_error의 기본 <stdexcept>
13
 언제까지 ? catch 를 만날때까지
15
16
void f1() { throw 0; }
void f2() { f1(); }
void f3() { f2(); }
void f4() { f3(); }
void main()
{
try
{
f4();
}
catch (int e)
{
std::cout << e << std::endl;
}
}
 예외 처리를 하기 위해 발생 시점부터 처리하는 위치까지 Stack에서 함수를 소멸시키면서 이동
함수 호출
스택 풀기

Weitere ähnliche Inhalte

Was ist angesagt?

14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
유석 남
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
Seok-joon Yun
 
Regex & property sheet
Regex & property sheetRegex & property sheet
Regex & property sheet
Youngkwon Lee
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 

Was ist angesagt? (20)

Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
 
Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04Unity Surface Shader for Artist 04
Unity Surface Shader for Artist 04
 
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
Startup JavaScript 5 - 객체(Date, RegExp, Object, Global)
 
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
 
2. c언어의 기본
2. c언어의 기본2. c언어의 기본
2. c언어의 기본
 
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
 
javascript02
javascript02javascript02
javascript02
 
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
 
Effective modern cpp item14
Effective modern cpp item14Effective modern cpp item14
Effective modern cpp item14
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
 
[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식
 
Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03Unity Surface Shader for Artist 03
Unity Surface Shader for Artist 03
 
Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02Unity Surface Shader for Artist 02
Unity Surface Shader for Artist 02
 
Regex & property sheet
Regex & property sheetRegex & property sheet
Regex & property sheet
 
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
Javascript hoisting
Javascript hoistingJavascript hoisting
Javascript hoisting
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 

Ähnlich wie [KOSSA] C++ Programming - 13th Study - exception handling

[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
Sang Don Kim
 
읽기 좋은 코드가 좋은 코드다 Part one
읽기 좋은 코드가 좋은 코드다   Part one읽기 좋은 코드가 좋은 코드다   Part one
읽기 좋은 코드가 좋은 코드다 Part one
Ji Hun Kim
 
공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin
knight1128
 
Api design for c++ 6장
Api design for c++ 6장Api design for c++ 6장
Api design for c++ 6장
Ji Hun Kim
 

Ähnlich wie [KOSSA] C++ Programming - 13th Study - exception handling (20)

[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
 
RNC C++ lecture_2 Variable DataType
RNC C++ lecture_2 Variable DataTypeRNC C++ lecture_2 Variable DataType
RNC C++ lecture_2 Variable DataType
 
C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부C++ 11 에 대해서 쉽게 알아봅시다 1부
C++ 11 에 대해서 쉽게 알아봅시다 1부
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
Boost라이브러리의내부구조 20151111 서진택
Boost라이브러리의내부구조 20151111 서진택Boost라이브러리의내부구조 20151111 서진택
Boost라이브러리의내부구조 20151111 서진택
 
C review
C  reviewC  review
C review
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
[Td 2015]녹슨 c++ 코드에 모던 c++로 기름칠하기(옥찬호)
 
강의자료4
강의자료4강의자료4
강의자료4
 
3.포인터
3.포인터3.포인터
3.포인터
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, For
 
Visual studio 2010
Visual studio 2010Visual studio 2010
Visual studio 2010
 
[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기[NDC2016] TERA 서버의 Modern C++ 활용기
[NDC2016] TERA 서버의 Modern C++ 활용기
 
C++11
C++11C++11
C++11
 
읽기 좋은 코드가 좋은 코드다 Part one
읽기 좋은 코드가 좋은 코드다   Part one읽기 좋은 코드가 좋은 코드다   Part one
읽기 좋은 코드가 좋은 코드다 Part one
 
C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2C++17 Key Features Summary - Ver 2
C++17 Key Features Summary - Ver 2
 
공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin공유 Jdk 7-2-project coin
공유 Jdk 7-2-project coin
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
Api design for c++ 6장
Api design for c++ 6장Api design for c++ 6장
Api design for c++ 6장
 

Mehr von Seok-joon Yun

[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
Seok-joon Yun
 

Mehr von Seok-joon Yun (20)

Retrospective.2020 03
Retrospective.2020 03Retrospective.2020 03
Retrospective.2020 03
 
Sprint & Jira
Sprint & JiraSprint & Jira
Sprint & Jira
 
Eks.introduce.v2
Eks.introduce.v2Eks.introduce.v2
Eks.introduce.v2
 
Eks.introduce
Eks.introduceEks.introduce
Eks.introduce
 
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image ConverterAWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
AWS DEV DAY SEOUL 2017 Buliding Serverless Web App - 직방 Image Converter
 
아파트 시세,어쩌다 머신러닝까지
아파트 시세,어쩌다 머신러닝까지아파트 시세,어쩌다 머신러닝까지
아파트 시세,어쩌다 머신러닝까지
 
Pro typescript.ch07.Exception, Memory, Performance
Pro typescript.ch07.Exception, Memory, PerformancePro typescript.ch07.Exception, Memory, Performance
Pro typescript.ch07.Exception, Memory, Performance
 
Doing math with python.ch07
Doing math with python.ch07Doing math with python.ch07
Doing math with python.ch07
 
Doing math with python.ch06
Doing math with python.ch06Doing math with python.ch06
Doing math with python.ch06
 
Doing math with python.ch05
Doing math with python.ch05Doing math with python.ch05
Doing math with python.ch05
 
Doing math with python.ch04
Doing math with python.ch04Doing math with python.ch04
Doing math with python.ch04
 
Doing math with python.ch03
Doing math with python.ch03Doing math with python.ch03
Doing math with python.ch03
 
Doing mathwithpython.ch02
Doing mathwithpython.ch02Doing mathwithpython.ch02
Doing mathwithpython.ch02
 
Doing math with python.ch01
Doing math with python.ch01Doing math with python.ch01
Doing math with python.ch01
 
Pro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScriptPro typescript.ch03.Object Orientation in TypeScript
Pro typescript.ch03.Object Orientation in TypeScript
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
[2015-07-20-윤석준] Oracle 성능 관리 2
[2015-07-20-윤석준] Oracle 성능 관리 2[2015-07-20-윤석준] Oracle 성능 관리 2
[2015-07-20-윤석준] Oracle 성능 관리 2
 
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
 
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
 
오렌지6.0 교육자료
오렌지6.0 교육자료오렌지6.0 교육자료
오렌지6.0 교육자료
 

[KOSSA] C++ Programming - 13th Study - exception handling

  • 1.
  • 2.
  • 3. 3 try { if (/* Exception Condition */) throw new std::exception("Error Description"); } catch (std::exception e) { std::cout << "Exception : " << e.what() << std::endl; }
  • 4. 4 try { if (/* Exception Condition */) throw new std::exception("Error Description"); } catch (std::exception e) { cout << "Exception : " << e.what() << endl; } 예외가 발생할 수 있는 부분을 정의 try { } 와 catch { } 는 한쌍 예외를 발생시킴 try 안에서 발생한 예외 중 e를 catch 예외 처리
  • 5. 5 #include <iostream> void main() { int a = 9; int b = 0; int c = a / b; std::cout << c << std::endl; }
  • 6. void main() { int a = 9; int b = 0; int c = 0; if (b != 0) { c = a / b; std::cout << c << std::endl; } else std::cout << "Exception : Divide by zero" << std::endl; } 6 원래 Code와 예외처리 부분이 뒤죽박죽
  • 7. void main() { int a = 9; int b = 0; int c = 0; try { if (b == 0) throw b; c = a / b; std::cout << c << std::endl; } catch (int divided) { std::cout << "Exception : Divide by " << divided << std::endl; } } 7 좀 더 명확하게 구분가능 (이라고 주장하기엔 앞 Page 예제랑 별 차이가…)
  • 8. void main() { int a = 9; int b = 0; int c = 0; try { c = Divide<int>(a, b); std::cout << c << std::endl; } catch (int divided) { std::cout << "Exception : Divide by " << divided << std::endl; } } 8 함수를 사용하면서 예외 처리 할려면… template <typename T> T Divide(T a, T b) { if (b == 0) throw std::exception("Divide by zero"); return a / b; }
  • 9. 9 • void func(int a) • void func(int a) throw(int); • void func(int a) throw(char *, int); • void func(int a) throw(); • void func(int a) noexcept; 모든 타입의 예외가 발생 가능하다. int 타입 예외를 던질 수 있다. 타입이 2가지 이상일 경우는 , 로 나열 예외를 발생하지 않는다. C++11 Style
  • 10.
  • 11.  #include <exception>하면 대부분의 경우 사용 가능 11
  • 12. 12 #include <iostream> #include <new> void main() { char* ptr; try { ptr = new char[(~unsigned int((int)0) / 2) - 1]; delete[] ptr; } catch (std::bad_alloc &ba) { std::cout << ba.what() << std::endl; } }
  • 13. • bad_alloc: 메모리 할당 오류로서 new 연산에서 발생 <new> • bad_cast : 형변환 오류로서 dynamic_cast 에서 발생 <typeinfo.h> • bad_type_id : typeid에 대한 피 연산자가 널 포인터인 경우 발생 • bad_exception : 예기치 못한 예외로서 함수 발생 목록에 있지 않는 예외 • bad_logic_error : 클래스 논리 오류 • invalid_argument, length_error, out_of_range 의 기본 <stdexcept> • runtime_error : 실행 오류로 overflow_error, underflow_error의 기본 <stdexcept> 13
  • 14.
  • 15.  언제까지 ? catch 를 만날때까지 15
  • 16. 16 void f1() { throw 0; } void f2() { f1(); } void f3() { f2(); } void f4() { f3(); } void main() { try { f4(); } catch (int e) { std::cout << e << std::endl; } }  예외 처리를 하기 위해 발생 시점부터 처리하는 위치까지 Stack에서 함수를 소멸시키면서 이동 함수 호출 스택 풀기