Basic try-except structure
Thetry block contains code that might fail. The except block runs if an error happens:
Catching specific errors
Different errors need different handling. Catch specific error types:Multiple error types
Handle different errors differently:The else clause
Code inelse runs only if no error happened:
The finally clause
Code infinally always runs, error or not:
Common mistakes
Catching too broad exceptions
Catching too broad exceptions
Empty except blocks
Empty except blocks
Not re-raising critical errors
Not re-raising critical errors
What’s next?
Now let’s look at the most common errors you’ll encounter in Python.Common errors
Errors you’ll see often