Exception Hierarchy
Let's return to the OOP topic. The Exception is a class that has different instances.
12345678910111213print("=== FIRST CASE ===") try: "string" + 5 # string + int except TypeError as error: print("TypeError:", error) print("Is instance of TypeError:", isinstance(error, TypeError)) print("=== SECOND CASE ===") try: "string" / 10 # string / int except TypeError as error: print("TypeError:", error) print("Is instance of TypeError:", isinstance(error, TypeError))
In the example above, the error is a variable that contains the TypeError instance.
| First Case | Second Case | |
|---|---|---|
| Class | TypeError | TypeError |
| Action for exception | str + int | str / int |
| Message | can only concatenate str (not "int") to str | unsupported operand type(s) for /: 'str' and 'int' |
So, we have different instances of the TypeError class with different messages
Exception Hierarchy
The Exception classes have a class hierarchy:
Every Exception class is inherited from the BaseException class.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Posez-moi des questions sur ce sujet
Résumer ce chapitre
Afficher des exemples du monde réel
Génial!
Completion taux amélioré à 11.11
Exception Hierarchy
Glissez pour afficher le menu
Let's return to the OOP topic. The Exception is a class that has different instances.
12345678910111213print("=== FIRST CASE ===") try: "string" + 5 # string + int except TypeError as error: print("TypeError:", error) print("Is instance of TypeError:", isinstance(error, TypeError)) print("=== SECOND CASE ===") try: "string" / 10 # string / int except TypeError as error: print("TypeError:", error) print("Is instance of TypeError:", isinstance(error, TypeError))
In the example above, the error is a variable that contains the TypeError instance.
| First Case | Second Case | |
|---|---|---|
| Class | TypeError | TypeError |
| Action for exception | str + int | str / int |
| Message | can only concatenate str (not "int") to str | unsupported operand type(s) for /: 'str' and 'int' |
So, we have different instances of the TypeError class with different messages
Exception Hierarchy
The Exception classes have a class hierarchy:
Every Exception class is inherited from the BaseException class.
Merci pour vos commentaires !