프로그래밍의 꽃(?) 중 하나는 에러 처리다.
많은 에러가 있지만, 기초 내용 정리를 위해 작성해보는 대표적인 error list !
(정확히는 error(syntax error), exceptions로 나뉘지만 구분 없이 list up 했다)
Syntax Error
"Syntax errors, also known as parsing errors...
The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected." (공식문서)
Exceptions
"Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it." (공식문서)
1. Invalid syntax
for
>>> SyntaxError: invalid syntax
2. NameError
a = b
>>> NameError: name 'b' is not defined
3. assign to literal
list/dict/set/tuple
자료형을 말함 None
은 특수 리터럴에 해당 100 = 'hello'
>>> SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
4. KeyError
myDict = {'apple':3, 'orange':10, 'lemon':5}
print(myDict['grape'])
>>> KeyError: 'grape'
5. ValueError
int(3.14)
>>> ValueError: invalid literal for int() with base 10: '3.14'
6. TypeError
a = 'hello'
b = 3.14
print(a+b)
>>> TypeError: can only concatenate str (not "float") to str
7. IndexError
lst = [1,2,3]
for i in (0,5):
print(lst[i])
>>> IndexError: list index out of range
8. ImportError : module은 존재하나 클래스/함수 등이 존재하지 않는 경우
from copy import python
>>> ImportError: cannot import name 'python' from 'copy' (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/copy.py)
PI = 3.14
True
, False
) None
myList = [1,2,3]
myTuple = (1,2,3)
mySet = {1,2,3}
myDict = {'a':1, 'b':2, 'c':3}
참고 자료
파이썬 공식문서
https://wikidocs.net/20562