Python - Else - Finally
Code Life
By Pierre Marsaa
Introduction to the else Clause
We saw try and except for handling errors. Now let's learn about the else clause!
The else clause in a try/except block is an optional part that runs only when no exception occurs in the try block.
Basic syntax:
Loading editor...
Let's see it in action with a Codémon example:
↗️
Loading editor...
↶
↷
Why use else instead of putting code at the end of try?
The else clause makes your code clearer by separating the risky operation (in try) from the success actions (in else). It also ensures that only exceptions from the try block are caught, not from your success code.
Verify