Python Try Except

When an error occurs in your code, Python stops executing your code and generates an error message. In Python, an error is called an exception.

Here is a quick example to demonstrate this:

The variable doesn’t exist, so Python throws a NameError exception. Our program stops so it never prints the “Testing exceptions” message. Here is one more example:

Above, we see we get a TypeError exception because you can’t concatenate a string and an integer. Python has a list of built-in exceptions.

Try Except

The two examples above deal with exceptions in our own code. We could easily fix these, preventing those exceptions in the first place. This is not always possible though. For example, when you connect with an external network device through SSH or an API, it’s possible that the connection sometimes fails. We have to take this into account when we write our code.

We can use a try/except block. We add the code we want to run under the try block. When it fails, the except block handles the exception. Let me show you an example:

The variable we try to print doesn’t exist, so our code hits the except block.

Multiple Exceptions

In the example above we handled our exception but it was a “catch-all” except block. It’s also possible to deal with exceptions based on the exception error. Let me show you an example:

In the code above, we have an except block for NameError and the general except block. It hits our first except block because we ran into a NameError exception.

Else

If you want to do something when no exceptions are raised, you can add an additional else statement. Here is an example:

The try block didn’t run into exceptions and we executed the else statement. Here is one more example:

The code above will raise an exception because the variable I specify under the try block doesn’t exist. The else statement doesn’t run, because we hit the except block.

Finally

We can also run something in the end, independent from whether we hit the try or except block. We do this with the finally statement. Here is an example:

In the example above, we hit the try block and we print something because of the finally statement. Here is one more example:

This time we hit the except block but we still run whatever code we have after the finally statement.

Raise Exception

When you create your own programs, it’s also possible to generate your own exception error messages with the raise keyword. Here’s how:

This raises a general exception with our message. It’s better to include the exact exception though. For example:

Unlock This Lesson for Free - No Payment Required!

If you like to keep on reading, register now!

  • Learn CCNA, CCNP and CCIE R&S. Explained As Simple As Possible.
  • Get Instant Access to this Full Lesson, Completely for Free!
  • Unlock More to Read. More Lessons Added Every Week!
  • Content created by Rene Molenaar (CCIE #41726)
2564 Sign Ups in the last 30 days

Ask a question or start a discussion by visiting our Community Forum