NameError: name 'a' is not defineda = a + 1
ZeroDivisionError: division by zero>>>a = 0
>>>b = 1
>>>c = b / a
IndexError: list index out of range>>a = [1,2]
>>a[2]
TypeError: write() argument must be str, not int>>>f=open(‘a.txt’, ‘w’)
>>>f.write(123)
: try 안의 내용을 실행한 후 에러가 발생하면 except 안의 내용 실행
try:
a = a + 1
a = 0
c = 1 / a
a = [1,2]
a[2]
f = open(‘.txt’, ‘w’)
f.write(123)
f.close()
except:
print(‘Exception’)
try:
#code block
except TypeError:
print(‘Type Error’)
except ZeroDivisionError:
print('zero-division exception')
except FileNotFoundError:
print('file not found exception')
else : 예외가 어나지 않는 경우finally : 예외 여부에 상관없이 꼭 처리해야 할 것이 있는 경우import sys
try:
a = 1
a = a + 1
except Exception as e:
print(e)
sys.exit()
else:
print('no error')
finally:
print('mandatory execution code')