🐍 파이썬/파이썬 기본 문법

[에러/예외처리] Error handling

써니(>_<) 2022. 8. 9. 12:15

Python Built-in Exceptions

  • IndexError: list index out of range
  • ValueError: invalid literal for (e.g. int("a"))
  • NameError: name is not defined
  • ZeroDivisionError: division by zero
  • KeyError: a dictionary key is not found

Raising Exceptions

def do_something_with_number(n):
 if n < 0 :
 	raise ValueError("negative number is given")

Handling Exception : try / exception statement

Code that can cause an exception is included in a try block.