Skip to main content

Python Try Except

Four blocks work together when you deal with errors. try holds the code you want to test for problems, and except deals with the error if one appears.

else runs only when no error happened, while finally runs whatever the outcome of the try and except blocks.


Exception Handling

An error, also called an exception, normally makes Python stop and print an error message.

The try statement lets you catch these exceptions and handle them yourself:

Here the try block fails, so Python moves on to the except block.

Remove the try block and the same error crashes the program:

Example

x has no definition, so the try block ends in an exception:

Prompt
Write a Python program that uses a try/except block. Try to print an undefined variable x, and catch the exception.
Output: complete Python program

Example

With no try around it, this line fails because x has no definition:

Prompt
Write a Python program that tries to print an undefined variable x. This will raise a NameError.
Output: complete Python program

Many Exceptions

A single try can be followed by any number of except blocks, which is handy when one kind of error needs its own special code:

Example

Show one message for a NameError and a different one for anything else:

Prompt
Write a Python program with multiple except blocks. Try to print undefined variable x, catch NameError specifically and other errors generally.
Output: complete Python program

Else

Add the else keyword for code that should run only when the try block raised no errors:

Example

Nothing fails in this try block, so else gets its turn:

Prompt
Write a Python program using try/except/else. The try block prints "Hello" successfully, so the else block executes.
Output: complete Python program

Finally

A finally block, when you include one, always runs, whether or not the try block raised an error.

That makes it a good place for closing objects and cleaning up resources:

The file object does not stay open, and the program is free to carry on.

Example

Prompt
Write a Python program using try/except/finally. Try to print undefined variable x, handle the exception, and use finally.
Output: complete Python program

Example

Open a file and attempt to write to it, with separate handling for each step. In this run, the file could not even be opened:

Prompt
Write a Python program that tries to open and write to a file. Use nested try/except/finally to handle file errors and ensure the file is closed.
Output: complete Python program

Raise an exception

Sometimes your own code should signal a problem when a certain condition is met. The raise keyword does exactly that: it throws (raises) an exception.

You decide both the type of error and the message shown to the user.

Example

Stop the program with an error when x is negative:

Prompt
Write a Python program that raises an exception. Set x to -1 and raise an Exception if x is lower than 0.
Output: complete Python program

Example

Raise a TypeError unless x is an integer:

Prompt
Write a Python program that raises a TypeError. Set x to "hello" and raise a TypeError if x is not an integer.
Output: complete Python program