Skip to main content

Basic try-except structure

The try 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 in else runs only if no error happened:

The finally clause

Code in finally always runs, error or not:
Don’t catch all errors with a bare except:. It hides problems and makes debugging harder. Always catch specific error types when possible.

Common mistakes

What’s next?

Now let’s look at the most common errors you’ll encounter in Python.

Common errors

Errors you’ll see often