PYTHON: How to Use Try and Except


You know how sometimes when the charger on your phone gets old, it won’t work unless it’s leaned at a 35 degree angle to the right while the device is hanging upside down from the cord? Duck tape. Duck tape is how I solve that problem. Same deal when the fridge handle breaks or when my rain coat gets holes or when my laptop screen gets dislodged from the body.

Duck tape.

Try and Except is the duck tape of Python. Everyone should be aware of its power.

Like duck tape, try/except doesn’t exactly fix things. It helps hold the code together when errors poke up to break it.

Let’s talk about hot dogs.

Say I wanted to write a script that’ll tell a user how many hot dogs per week they’re worth. It uses two things to figure this out—the number of hours per week you work and your hourly rate. It assumes a hot dog is about $2.00. Here’s what that might look like:

Sample Script

Now, the first time around, our user used the script as intended and fed it numbers. It gobbled those up and gave us an answer of 300 hot dogs.

The second time, our user was drunk and fed the script question marks. Python expressed its discomfort with that input by spitting out a bunch of angry red lettering.

I don’t want Python to throw an error here. I want it to gently remind the user that if they enter anything that’s not a value, they’re doing it wrong.

A Basic Try/Except Version

To make this work a little easier, I put the bulk of the hot dog math into a function called HowManyHotDogsAmIWorth. Within that function, I started off with the piece of code that does work under try.

Notice that the previous script’s problem ended in a ValueError. We can plug that specific error into the except block with lines of code telling it what to do if the user causes that error again.

Try Except

And now we have a more user-friendly, happy version.

A Multi-Error Version

Ok, a different script now. This one does the math based on your hourly rate, and you have to fill in how much hot dogs are.

You’ve probably guessed where this is heading…


If the user is hell-bent on getting an error, they can pop 0 in as the price of hot dogs. Since you can’t divide by zero. You get a ZeroDivisionError.

However, using another except block, we can duct tape that problem too. This time, it’ll tell the user that hot dogs are a wonderful thing and cannot be had for free.

A Hot Dog Script

And there you go. In theory you can program as many except blocks as you need to get the script to plow through just about any problem. You can take a look at more potential errors in the Python documentation.

Stay tuned for more Python posts soon.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s