Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Exception Hierarchy | Exception Usage
Error Handling in Python
course content

Kursinnehåll

Error Handling in Python

Error Handling in Python

1. Handling Structure
2. Exception Usage

book
Exception Hierarchy

Let's return to the OOP topic. The Exception is a class that has different instances.

12345678910111213
print("=== 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))
copy

In the example above, the error is a variable that contains the TypeError instance.

First CaseSecond Case
ClassTypeErrorTypeError
Action for exceptionstr + intstr / int
Messagecan only concatenate str (not "int") to strunsupported 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.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

Error Handling in Python

Error Handling in Python

1. Handling Structure
2. Exception Usage

book
Exception Hierarchy

Let's return to the OOP topic. The Exception is a class that has different instances.

12345678910111213
print("=== 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))
copy

In the example above, the error is a variable that contains the TypeError instance.

First CaseSecond Case
ClassTypeErrorTypeError
Action for exceptionstr + intstr / int
Messagecan only concatenate str (not "int") to strunsupported 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.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 2
Vi beklagar att något gick fel. Vad hände?
some-alt