Skip to content

Latest commit

ย 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

README.md

CPP Module 05

์˜ˆ์™ธ ์ฒ˜๋ฆฌ(Exception Handling)์™€ ์ค‘์ฒฉ ํด๋ž˜์Šค(Nested Class)๋ฅผ ํ•™์Šตํ•˜๋Š” ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ๊ด€๋ฃŒ์ œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ํ†ตํ•ด ์˜ˆ์™ธ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›๋‹ˆ๋‹ค.

์ฃผ์š” ํ•™์Šต ํฌ์ธํŠธ:

  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ (try, catch, throw)
  • ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค
  • ์ค‘์ฒฉ ํด๋ž˜์Šค (Nested Class)
  • ์˜ˆ์™ธ ์•ˆ์ „์„ฑ (Exception Safety)
  • RAII (Resource Acquisition Is Initialization)

๊ตฌ์„ฑ

ex00: Mommy, when I grow up, I want to be a bureaucrat!

  • Bureaucrat ํด๋ž˜์Šค ๊ตฌํ˜„
  • ์†์„ฑ: name (const), grade (1-150)
  • ๋“ฑ๊ธ‰ ๋ฒ”์œ„ ๊ฒ€์ฆ ๋ฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
  • ์ค‘์ฒฉ ์˜ˆ์™ธ ํด๋ž˜์Šค: GradeTooHighException, GradeTooLowException
  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜: incrementGrade, decrementGrade

ex01: Form up, maggots!

  • Form ํด๋ž˜์Šค (ํ›„์— AForm์œผ๋กœ ๋ณ€๊ฒฝ)
  • ์†์„ฑ: name (const), signed (bool), gradeToSign, gradeToExecute
  • beSigned() ํ•จ์ˆ˜๋กœ ์„œ๋ช… ์ฒ˜๋ฆฌ
  • Bureaucrat์˜ signForm() ํ•จ์ˆ˜
  • ์˜ˆ์™ธ: GradeTooHighException, GradeTooLowException

ex02: No, you need form 28B, not 28C...

  • AForm ์ถ”์ƒ ํด๋ž˜์Šค
  • ๊ตฌ์ฒด ํด๋ž˜์Šค: ShrubberyCreationForm, RobotomyRequestForm, PresidentialPardonForm
  • execute() ์ˆœ์ˆ˜ ๊ฐ€์ƒ ํ•จ์ˆ˜
  • ๊ฐ ํผ์˜ ๊ณ ์œ  ์‹คํ–‰ ๋กœ์ง
  • ์‹คํ–‰ ์กฐ๊ฑด ๊ฒ€์ฆ

ex03: At least this beats coffee-making

  • Intern ํด๋ž˜์Šค
  • makeForm() ํ•จ์ˆ˜๋กœ ๋™์  ํผ ์ƒ์„ฑ
  • ํผ ์ด๋ฆ„์œผ๋กœ ์ ์ ˆํ•œ ํผ ๊ฐ์ฒด ์ƒ์„ฑ
  • ํŒฉํ† ๋ฆฌ ํŒจํ„ด (Factory Pattern) ํ™œ์šฉ

ํ•ต์‹ฌ ๊ฐœ๋…

์˜ˆ์™ธ ์ฒ˜๋ฆฌ (Exception Handling)

  • try-catch ๋ธ”๋ก์œผ๋กœ ์˜ˆ์™ธ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌ
  • throw๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ , catch์—์„œ ํฌ์ฐฉํ•˜์—ฌ ์ฒ˜๋ฆฌ
  • std::exception์„ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ์ƒ์„ฑ

์ค‘์ฒฉ ํด๋ž˜์Šค (Nested Class)

  • ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์˜๋œ ํด๋ž˜์Šค
  • ์™ธ๋ถ€ ํด๋ž˜์Šค์™€ ๋ฐ€์ ‘ํ•œ ๊ด€๋ จ์ด ์žˆ์„ ๋•Œ ์‚ฌ์šฉ
  • GradeTooHighException, GradeTooLowException ๋“ฑ์„ Bureaucrat ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์ •์˜

์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ˆœ์ˆ˜ ๊ฐ€์ƒ ํ•จ์ˆ˜

  • ์ˆœ์ˆ˜ ๊ฐ€์ƒ ํ•จ์ˆ˜ (= 0)๋ฅผ ํฌํ•จํ•œ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šคํ™” ๋ถˆ๊ฐ€
  • ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•ด์•ผ ํ•จ
  • AForm ํด๋ž˜์Šค๊ฐ€ ์ถ”์ƒ ํด๋ž˜์Šค, execute()๊ฐ€ ์ˆœ์ˆ˜ ๊ฐ€์ƒ ํ•จ์ˆ˜

ํผ ํด๋ž˜์Šค ๊ตฌ์กฐ

  • ShrubberyCreationForm: ASCII ํŠธ๋ฆฌ๋ฅผ ํŒŒ์ผ์— ์ž‘์„ฑ (sign 145, exec 137)
  • RobotomyRequestForm: 50% ํ™•๋ฅ ๋กœ ๋กœ๋ด‡ํ™” ์‹œ๋„ (sign 72, exec 45)
  • PresidentialPardonForm: ๋Œ€ํ†ต๋ น ์‚ฌ๋ฉด ๋ฉ”์‹œ์ง€ ์ถœ๋ ฅ (sign 25, exec 5)

ํŒฉํ† ๋ฆฌ ํŒจํ„ด

  • Intern ํด๋ž˜์Šค๊ฐ€ ํผ ์ด๋ฆ„์„ ๋ฐ›์•„ ์ ์ ˆํ•œ ํผ ๊ฐ์ฒด๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑ
  • ๊ฐ์ฒด ์ƒ์„ฑ ๋กœ์ง์„ ์บก์Аํ™”ํ•˜์—ฌ ์ฝ”๋“œ ์œ ์—ฐ์„ฑ ํ–ฅ์ƒ
  • ํ•จ์ˆ˜ ํฌ์ธํ„ฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํผ ์ƒ์„ฑ ํ•จ์ˆ˜ ๋งคํ•‘

์˜ˆ์™ธ ์•ˆ์ „์„ฑ

  • Basic guarantee: ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์œ ํšจํ•œ ์ƒํƒœ ์œ ์ง€
  • Strong guarantee: ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ์ƒํƒœ ๋ณ€๊ฒฝ ์—†์Œ
  • No-throw guarantee: ์˜ˆ์™ธ๋ฅผ ๋˜์ง€์ง€ ์•Š์Œ

์ปดํŒŒ์ผ

c++ -Wall -Wextra -Werror -std=c++98 *.cpp -o bureaucrat

๊ฐ exercise์—์„œ:

make

์ฃผ์˜์‚ฌํ•ญ

  • ์˜ˆ์™ธ ํด๋ž˜์Šค ์ƒ์†: std::exception ์ƒ์†
  • what() ํ•จ์ˆ˜: const throw() ์ง€์ •์ž ์‚ฌ์šฉ (C++98)
  • const ์ •ํ™•์„ฑ: const ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ์—์„œ ์ดˆ๊ธฐํ™”
  • ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ: ๋™์  ํ• ๋‹น๋œ ํผ ๊ฐ์ฒด ํ•ด์ œ
  • ๋“ฑ๊ธ‰ ๊ฒ€์ฆ: ์ƒ์„ฑ์ž์™€ ๋ฉค๋ฒ„ ํ•จ์ˆ˜์—์„œ ํ•ญ์ƒ ๋ฒ”์œ„ ํ™•์ธ
  • Orthodox Canonical Form: ๋ณต์‚ฌ ์ƒ์„ฑ์ž, ๋Œ€์ž… ์—ฐ์‚ฐ์ž ๊ตฌํ˜„