# try, except : 에러가 발생해도 코드의 실행을 계속하고 싶을때 ls = [1, 2, 3]
1 2
print(ls[3]) print("Done!")
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-4-f88f334466aa> in <module>
----> 1 print(ls[3])
2 print("Done!")
IndexError: list index out of range
1 2 3 4 5 6
try: print(ls[3]) except Exception as e: print("error") print(e) print("Done")
error
list index out of range
Done
1 2 3 4 5 6 7
# finally : try, except 구문 실행된 후 finally 구문이 실행 try: 1/0 except: print("error") finally: print("Done!")
error
Done!
1
# raise : 강제로 에러를 발생시키는 명령
1 2 3 4 5 6 7
try: 1/0 except Exception as e: print("error") raise(e)
print("Done!")
error
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-9-bcde7d6d4636> in <module>
3 except Exception as e:
4 print("error")
----> 5 raise(e)
6
7 print("Done!")
<ipython-input-9-bcde7d6d4636> in <module>
1 try:
----> 2 1/0
3 except Exception as e:
4 print("error")
5 raise(e)
ZeroDivisionError: division by zero
1 2
# 에러 생성 : 10이상의 숫자가 입력되도록 하는 에러 # LowNumber
1 2 3 4
classLowNumber(Exception): def__str__(self): return"Number grater than 10"
1 2 3 4
definput_number(num): if num <= 10: raise LowNumber() print(num)
1
input_number(8)
---------------------------------------------------------------------------
LowNumber Traceback (most recent call last)
<ipython-input-15-8684a0e33c0b> in <module>
----> 1 input_number(8)
<ipython-input-13-2dcc4faaa999> in input_number(num)
1 def input_number(num):
2 if num <= 10:
----> 3 raise LowNumber()
4 print(num)
LowNumber: Number grater than 10