ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ค ๋ณด๋ฉด ์์์ด ๋ง์ ์๋ฌ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ฌผ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ ํ๋ก๊ทธ๋จ์ด ์๋ชป ๋์๋๋ ๊ฒ์ ๋ง๊ธฐ ์ํ ๊ฒ์ ๋๋ค. Python์ Python ํ๋ก๊ทธ๋จ์ ์์์น ๋ชปํ ์ค๋ฅ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๋๋ฒ๊น ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ ๋ ๊ฐ์ง ์ค์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
- ์์ธ ์ฒ๋ฆฌ(Exception Handling)
- ์ด์ค์ (Assertions)
ํ๋ก๊ทธ๋จ์ ํน์ ์ง์ ์์ ๋ฐ๋์ ์ฐธ(True)์ด ๋์ด์ผ ํ๋ ๋ฌธ์ฅ์ ๋งํฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ค๋ฅ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์ฝ๋์ Assertion์ ๋ฃ์ต๋๋ค. Assertion์ด ์คํจํ๋ฉด (์ฆ, ํํ์์ด ๊ฑฐ์ง์ผ๋ก ํ๊ฐ๋๋ ๊ฒฝ์ฐ) ํ๋ก๊ทธ๋จ์ด ์ค์ง๋๊ณ ์ค๋ฅ ๋ฉ์์ง๊ฐ ํ์๋ฉ๋๋ค.
๋ค์์ ์์ ๋ฅผ ์คํํด ๋ณด๋ฉด ๊ฐ๋ ์ด ๋ช ํํด์ง๋๋ค.
import time
lists = [1,3,6,3,8,7,13,23,13,2,3.14,2,3,7]
def test(t):
print("ํ์ฌ ์ํ์ค์ธ ๊ฐ =" + str(t))
assert (type(t) is int), "์ฃผ์ด์ง ๊ฐ " + str(t) + " ์ ์ ์๊ฐ ์๋๋๋ค"
for i in lists:
test(i)
time.sleep(1)ํ์ฌ ์ํ์ค์ธ ๊ฐ =23
ํ์ฌ ์ํ์ค์ธ ๊ฐ =13
ํ์ฌ ์ํ์ค์ธ ๊ฐ =2
ํ์ฌ ์ํ์ค์ธ ๊ฐ =3.14
Traceback (most recent call last):
File "C:/Users/nains/PycharmProjects/OneBook/JustTest.py", line 9, in <module>
test(i)
File "C:/Users/nains/PycharmProjects/OneBook/JustTest.py", line 6, in test
assert (type(t) is int), "์ฃผ์ด์ง ๊ฐ " + str(t) + " ์ ์ ์๊ฐ ์๋๋๋ค"
AssertionError: ์ฃผ์ด์ง ๊ฐ 3.14 ์ ์ ์๊ฐ ์๋๋๋ค
Process finished with exit code 1
๋ฌธ๋ฒ๊ณผ ๊ด๋ จ๋ ์๋ฌ๋ ํ์ด์ฌ ์ธํฐํ๋ฆฌํฐ์์ ์๋ฌ๋ฉ์์ง๋ฅผ ํ์ํ์ฌ ๋๋ฒ๊น ์ด ๊ฐ๋ฅํฉ๋๋ค. ๋ฌธ์ฅ์ด๋ ํํ์์ด ๋ฌธ๋ฒ์ ์ผ๋ก ์ฌ๋ฐ๋ฅด๋ค ํ ์ง๋ผ๋, ์คํํ๋ ค๊ณ ํ๋ฉด ์๋ฌ๋ฅผ ์ผ์ผํฌ ์ ์์ต๋๋ค. ์คํ ์ค์ ๊ฐ์ง๋๋ ์๋ฌ๋ค์ _์์ธ(_Exception) ๋ผ๊ณ ํฉ๋๋ค. ์์ธ๋ ํ๋ก๊ทธ๋จ ์คํ ์ค์ ๋ฐ์ํ์ฌ ํ๋ก๊ทธ๋จ์ ๋ช ๋ น ํ๋ฆ์ ๋ฐฉํดํ๋ ์ด๋ฒคํธ์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก, ํ์ด์ฌ์ ๋์ฒํ ์ ์๋ ์ํฉ์ ์ฒํ๊ฒ ๋๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค. ์์ธ๋ ์ค๋ฅ๋ฅผ ๋ํ๋ด๋ Python ๊ฐ์ฒด์ ๋๋ค. ์์ธ๊ฐ ๋ฐ์๋๋ฉด ์ฆ์ ์์ธ๋ฅผ ์ฒ๋ฆฌํด์ผํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์ด์ฌ์ด ์ข ๋ฃ๋ฉ๋๋ค.
์์ธ์ฒ๋ฆฌ๋ฅผ ์ํด try, except๋ฌธ์ ์ฌ์ฉํฉ๋๋ค. try, except ๋ฌธ์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ๊ตฌ์กฐ๋ ์๋์ ๊ฐ์ต๋๋ค.
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block. ์์ธ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ ์์ฌ์ค๋ฌ์ด ์ฝ๋๊ฐ ์๋ ๊ฒฝ์ฐ try: ๋ธ๋ก์ ์์ฌ์ค๋ฌ์ด ์ฝ๋๋ฅผ ๋ฃ์ด ํ๋ก๊ทธ๋จ์ ๋ฐฉ์ด์ ์ผ๋ก ์ฝ๋ฉํฉ๋๋ค. try: ๋ธ๋ก ๋ค์์ except: ๋ฌธ์ ํฌํจ์ํค๊ณ ์์ธ ๋ฌธ์ ๋ฅผ ์ฒ๋ฆฌํ๋ ์ฝ๋ ๋ธ๋ก์ ์ถ๊ฐํฉ๋๋ค.
์ด ์์ ๋ ์ฐ๊ธฐ ๊ถํ์ ์ฃผ๊ณ ํ์ผ์ ์ด์ด ๊ธฐ๋ก ํ๋ฏ๋ก ์ ์ ์ ์ผ๋ก ์ฒ๋ฆฌ ๋ฉ๋๋ค.
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print("Error: can\'t find file or read data")
else:
print("Written content in the file successfully")
fh.close()์๋ ์์ ๋ ์ฐ๊ธฐ ๊ถํ์ด ์์ด ํ์ผ์ ์ด๊ณ ๊ธฐ๋ก์ ์๋ํ๋ฏ๋ก ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
try:
fh = open("testfile", "r")
fh.write("This is my test file for exception handling!!")
except IOError:
print("Error: can\'t find file or read data")
else:
print("Written content in the file successfully")์๋ ์ฝ๋์ ๊ฐ์ด finally ๋ฌธ์ ์ฌ์ฉ ํ ์๋ ์์ต๋๋ค.
try:
fh = open("testfile", "w")
try:
fh.write("This is my test file for exception handling!!")
finally:
print("Going to close the file")
fh.close()
except IOError:
print("Error: can\'t find file or read data")try ๋ธ๋ก์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด finally ๋ธ๋ก์ผ๋ก ์ฆ์ ์ ๋ฌ๋ฉ๋๋ค. finally ๋ธ๋ก์ ๋ชจ๋ ๋ช ๋ น๋ฌธ์ด ์คํ๋ ํ์ ์์ธ๊ฐ ๋ค์ ๋ฐ์ํ๊ณ try-except ๋ฌธ์ ๋ค์ ์์ ๊ณ์ธต์ ์์ผ๋ฉด exceptsatements์์ ์ฒ๋ฆฌ๋ฉ๋๋ค.
exception์๋ ์ถ๊ฐ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ์ธ์๊ฐ ์์ ์ ์์ต๋๋ค. ์ธ์์ ๋ด์ฉ์ ์์ธ์ ๋ฐ๋ผ ๋ค๋ฆ ๋๋ค. ๋ค์๊ณผ ๊ฐ์ด except ์ ์ ๋ณ์๋ฅผ ์ ๊ณตํ์ฌ ์์ธ์ ์ธ์๋ฅผ ์บก์ฒํฉ๋๋ค.
try:
You do your operations here;
......................
except ExceptionType, Argument:
You can print value of Argument here...๋จ์ผ exception์ ์ฒ๋ฆฌํ๊ธฐ์ํ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฒฝ์ฐ except ๋ฌธ์์ ์์ธ ์ด๋ฆ ๋ค์ ๋ณ์๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค. ์ฌ๋ฌ exception์ ํธ๋ํํ๋ ๊ฒฝ์ฐ exception ํํ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
์ด ๋ณ์๋ ๋๋ถ๋ถ exception์ ์์ธ์ ํฌํจํ๋ exception์ ๊ฐ์ ๋ฐ์ต๋๋ค. ๋ณ์๋ ํ๋์ ๊ฐ ๋๋ ํํ ํํ์ ๋ค์ค ๊ฐ์ ์์ ํ ์ ์์ต๋๋ค. ์ด ํํ์ ๋๊ฐ ์ค๋ฅ ๋ฌธ์์ด, ์ค๋ฅ ๋ฒํธ ๋ฐ ์ค๋ฅ ์์น๋ฅผ ํฌํจํฉ๋๋ค.
# Define a function here.
def temp_convert(var):
try:
return int(var)
except ValueError as Argument:
print("The argument does not contain numbers\n", Argument)
# Call above function here.
temp_convert("xyz");์์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๋ก ์ฒ๋ฆฌ ๋ฉ๋๋ค.
The argument does not contain numbers
invalid literal for int() with base 10: 'xyz'
raise ๋ฌธ์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ์์ธ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค. raise ๋ฌธ์ ๋ํ ์ผ๋ฐ์ ์ธ ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
raise [Exception [, args [, traceback]]]
์ฌ๊ธฐ์ Exception์ ์์ธ์ ์ ํ (์: Error๋ช )์ด๊ณ argument๋ ์์ธ ์ธ์ ๊ฐ์ ๋๋ค. ์ธ์๋ ์ ํ ์ฌํญ์ผ๋ก ์ ๊ณต๋์ง ์์ผ๋ฉด ์์ธ ์ธ์๋ ์๋๊ฒ์ผ๋ก ์ฒ๋ฆฌ๋ฉ๋๋ค.
๋ง์ง๋ง ์ธ์์ธ traceback์ ์ ํ์ ์ด๋ฉฐ (์ค์ ๋ก๋ ๊ฑฐ์ ์ฌ์ฉ๋์ง ์๋๋ค.), ์กด์ฌํ๋ค๋ฉด ์์ธ์ ์ฌ์ฉ๋๋ traceback ๊ฐ์ฒด์ ๋๋ค.
์์ธ๋ ๋ฌธ์์ด, ํด๋์ค ๋๋ ๊ฐ์ฒด๊ฐ ๋ ์ ์์ต๋๋ค. ํ์ด์ฌ ์ฝ์ด๊ฐ ์ ๊ธฐํ๋ ๋๋ถ๋ถ์ ์์ธ๋ ํด๋์ค์ด๋ฉฐ, ํด๋์ค์ ์ธ์คํด์ค์ธ ์ธ์๊ฐ ์์ต๋๋ค. ์๋ก์ด ์์ธ๋ฅผ ์ ์ํ๋ ๊ฒ์ ๋งค์ฐ ์ฝ๊ณ ๋ค์๊ณผ ๊ฐ์ด ํ ์ ์์ต๋๋ค.
def functionName( level ):
if level < 1:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception๋ค์๊ณผ ๊ฐ์ ์์ ๋ฅผ ์คํํด ๋ณด๊ฒ ์ต๋๋ค.
x = 5
if x < 10:
raise ValueError('x should not be less than 10!')