Exception क्लास है, जो StopIteration, StandardError और Warning का बेस क्लास है। सभी स्टैंडर्ड एरर्स StandardError से प्राप्त होते हैं। कुछ स्टैंडर्ड एरर्स जैसे ArithmeticError, AttributeError, AssertionError बेस क्लास StandardError से प्राप्त होते हैं।
जब एट्रिब्यूट रेफरेंस या असाइनमेंट विफल होता है, तो AttributeError उठाया जाता है। उदाहरण के लिए, जब किसी ऐसे एट्रिब्यूट को रेफरेंस करने का प्रयास किया जाता है जो मौजूद नहीं है:
हम दिए गए कोड को फिर से लिखते हैं और एक्सेप्शन पकड़ते हैं और उसका प्रकार जानते हैं।
उदाहरण
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('This is an example of StandardError exception')
आउटपुट
0 Foobar instance has no attribute 'q'This is an example of StandardError exception