๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Exception2

ํŒŒ์ด์ฌ ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception)์˜ ๋ชจ๋“  ๊ฒƒ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์™ธ ์ƒํ™ฉ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ์ƒํ™ฉ์„ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์—†๋Š” ํŒŒ์ผ ์—ด๊ธฐ >>> f = open("abc.txt", 'r') Traceback (most recent call last): File "", line 1, in FileNotFoundError: [Errno 2] No such file or directory: 'abc.txt' Zero division >>> 4 / 0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero index error >>> a = [1, 2, 3] >>> a[3] Traceback (most recent call last): File "", lin.. 2023. 12. 10.
[C++] 17. ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception), std::exception ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Exception) C++์—์„œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋Š” ํ”„๋กœ๊ทธ๋žจ์˜ ์‹คํ–‰ ์ค‘์— ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ ์ ˆํ•œ ์กฐ์น˜๋ฅผ ์ทจํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋žจ์˜ ์•ˆ์ •์„ฑ์„ ๋†’์ด๊ณ  ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๋ฌธ์ œ์— ๋Œ€์ฒ˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ์™ธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰ ์ค‘์— ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ๋˜๋Š” ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ์ƒํ™ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๊ธฐ๋ณธ์ ์ธ C++ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฉ”์ปค๋‹ˆ์ฆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ์™ธ ๋˜์ง€๊ธฐ (Throwing Exceptions) ์˜ˆ์™ธ๋ฅผ ๋˜์งˆ ๋•Œ๋Š” throw ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. throw ํ‚ค์›Œ๋“œ ๋’ค์—๋Š” ์˜ˆ์™ธ ๊ฐ์ฒด๊ฐ€ ์˜ต๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ˆ์™ธ ๊ฐ์ฒด๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ ์‚ฌ์šฉ์ž ์ •์˜ ํด๋ž˜์Šค ๊ฐ์ฒด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. void doSomething(int value) { if (value.. 2023. 7. 23.