Error là thứ đi sai trong chương trình tại compile time như syntactical error.
Ví dụ:
'abe' = 5
OUTPUT
SyntaxError: can't assign to literal
Ngay cả khi statement hoặc expression syntactically correct, nó có thể cause error khi attempt made để execute nó. Errors detected during execution gọi là exceptions và không phải luôn fatal. Nếu exceptions không được handle, error messages show up khi code executed hoặc run.
Về cơ bản, khi Python script gặp error mà nó không thể handle, nó raise exception và tạo exception object.
Thường thì script handle exception ngay lập tức. Nếu nó không làm vậy, chương trình sẽ terminate và print traceback đến error cùng với details. Ví dụ:
abe < 5
OUTPUT
Traceback (most recent call last): File "C:/Users/TutorialsPoint1/~.py", line 1, inabe < 5 NameError: name 'abe' is not defined
Exceptions convenient nhiều ways cho handling errors và special conditions trong chương trình. Khi bạn nghĩ bạn có code có thể produce error, bạn có thể dùng exception handling.