Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Detect and Catch | Handling Structure
Error Handling in Python
course content

Kursinnehåll

Error Handling in Python

Error Handling in Python

1. Handling Structure
2. Exception Usage

book
Detect and Catch

Now, let's start to learn the error handling structure.

To handle an error, you need to follow the following steps:

  1. Select the code block for detection using the try keyword.

  2. Catch an error using the except keyword.

Keywords

The try keyword defines the code block that executes inside the global scope of your program and the except keyword catch and error:

123456789101112131415
a = 15 b = 5 c = 10 try: a = a // 5 b = b // 0 c = c // 2 except: print("An error was caught.") print("a =", a) print("b =", b) print("c =", c)
copy

In a few words, the Python interpreter expects an error inside the try code block. If an error is raised, the interpreter executes the except code block.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. 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
Detect and Catch

Now, let's start to learn the error handling structure.

To handle an error, you need to follow the following steps:

  1. Select the code block for detection using the try keyword.

  2. Catch an error using the except keyword.

Keywords

The try keyword defines the code block that executes inside the global scope of your program and the except keyword catch and error:

123456789101112131415
a = 15 b = 5 c = 10 try: a = a // 5 b = b // 0 c = c // 2 except: print("An error was caught.") print("a =", a) print("b =", b) print("c =", c)
copy

In a few words, the Python interpreter expects an error inside the try code block. If an error is raised, the interpreter executes the except code block.

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

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