Detect and Catch
Now, let's start to learn the error handling structure.
To handle an error, you need to follow the following steps:
- Select the code block for detection using the
trykeyword. - Catch an error using the
exceptkeyword.
Keywords
The try keyword defines the code block that executes inside the global scope of your program and the except keyword catch and error:
123456789101112131415a = 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)
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.
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 11.11
Detect and Catch
Scorri per mostrare il menu
Now, let's start to learn the error handling structure.
To handle an error, you need to follow the following steps:
- Select the code block for detection using the
trykeyword. - Catch an error using the
exceptkeyword.
Keywords
The try keyword defines the code block that executes inside the global scope of your program and the except keyword catch and error:
123456789101112131415a = 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)
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.
Grazie per i tuoi commenti!