Có class Exception, là base class cho StopIteration, StandardError và Warning. Tất cả standard errors đều derived từ StandardError. Một số standard errors như ArithmeticError, AttributeError, AssertionError derived từ base class StandardError.
Khi attribute reference hoặc assignment thất bại, AttributeError được raise. Ví dụ, khi cố gắng reference attribute không tồn tại:
Chúng ta viết lại code cho trước và catch exception và biết type của nó.
Ví dụ
import sys
try:
class Foobar:
def __init__(self):
self.p = 0
f = Foobar()
print(f.p)
print(f.q)
except Exception as e:
print(e)
print(sys.exc_type)
print('Đây là ví dụ StandardError exception')
Kết quả
0 Foobar instance has no attribute 'q' <type 'exceptions.AttributeError'> Đây là ví dụ StandardError exception