Last Updated: Saturday 16th March 2013

Often times when coding a python masterpiece, there are certain things that could go wrong when executing your masterfully designed code. Things such as files or directories that are missing, empty strings, variables that are supposed to be strings but are actually arrays at run-time.

These things are called exceptions in Python. This is what the try keyword is for.

It allows execution of potentially breaking code that is nested in a comfortable block. This block will attempt to catch any of those nasty exceptions, and then execute code to either recover from the error, or notify the user of the breakage.

For example, this function would like to run:

But, as we can tell, it has the potential to raise an exception. When an exception is raised, and it's not in a try block with the proper exception handling, it will stop the execution of the code.

If this code is run as:

It will be fine and no one will know the difference.

But, if it's run as

We get this lovely message about the code not being able to succeed. Except that it's not that lovely. It's actually pretty ugly.

So what can we do?

That's where the try block comes in!

So, to make our above code run-time friendly, we can do this:

What did we just do? Well, randint will choose a random integer between the two given inputs. In this case, False is 0 and True is 1. So the succeed function will randomly raise an exception.

Now with the except part. We tell Python to execute handle_exception only if the succeed function raises an exception.

So if we run this code, the output will either be nothing, if we succeed, and if we don't:

But what if you want to execute a piece of code after a success? You could do this

Now, we see that if succeed doesn't raise an exception, then another_method_that_could_fail runs! Amazing! We did it!

But wait! If another_method_that_could_fail runs, it will run handle_exception again, and we want to print a different message. Darn.

So what do we do? Well, we could add another piece to our try block like so:

Okay, that's all well and good. But now, how do we run a piece of code on a successful run, with no exceptions? Well, there is a part of the try block that is not well known. It's the the else keyword. Observe:

If we take our previous code, and modify it a bit:

Then we get a nice message printed only when we succeed. Okay, but what if we need a piece of code to run no matter what? Useful for, say, closing a file that has been opened in the try block? Yes!

From stage left, enter the finally keyword. It will do exactly what we want.

Observe it in glorious action:

And Voilà! There we have it. Now we will always print the message in the finally section.

That's it for the try/except/else/finally block. I hope I educated you well in your quest to become a Python master.

Sayōnara, for now.

About The Author

  • Michael

    I think that you could better present how to use finally:

    In your example we could resign from finally and print and the result will be the same.

    Instead of that you could use this:

    except:
    sys.exit(2)
    finally:
    print (“Finally! I’m done. I don’t care if I failed or not. I’m DONE.”)

    • Jackson Cooper

      Hi Michael! Good idea, agreed. The article’s been updated.