HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Exceptions Lesson

Catch Python Exceptions

4 min to complete · By Martin Breuss

Contents

  1. Introduction

In the previous lesson, you collected input from a user and printed a message. If they didn't enter input, it could be converted to an integer.

For a small example like that, this might be an okay approach. However, you'll generally want to be as explicit and specific as possible with the exceptions that you're catching.

In this lesson, you'll see why it can be helpful to specify the type of exception that you want to handle, as well as how to do it.

Calculator App

Pure numbers can be discouraging for some people. So, you might want to build your own polite text-based calculator app. After all, you love it when your CLI prompts you in a polite manner for numerical input :)

Some of your code for this calculator app could look like this:

dividend = int(input("Please enter the number to be divided: "))
divisor = int(input("Please enter the divisor: "))
result = dividend / divisor
print(f"The result of {dividend} divided by {divisor} is {result}")

Such a polite calculator; it's truly humbling! This makes division a truly unifying experience!

However, there is more than one possible issue with this code. Things can go wrong, and it wouldn't be polite to leave your users without any indication of what happened! You already thought about the possibility that your users might enter a word instead of a number, but there's yet another danger lurking here.

Catch A Specific Python Exception

Dividing numbers by zero is not a thing in mathematics. If you've used Python as a calculator for a bit, you might have encountered the ZeroDivisionError already. This exception is raised by Python when you try to divide a number by zero.

In your polite calculator app, you'd need a way to handle what happens when your users try to divide a number by zero so that your program won't quit by raising a ZeroDivisionError.

You can do this by surrounding the code with a try except block and catching the ZeroDivisionError explicitly:

try:
    dividend = int(input("Please enter the number to be divided: "))
    divisor = int(input("Please enter the divisor: "))
    result = dividend / divisor
    print(f"The result of {dividend} divided by {divisor} is {result}")
except ZeroDivisionError:
    print("My most sincere apologies, but you can't divide by zero.")

If you catch Python's ZeroDivisionError like this, then the except block will politely handle any ZeroDivisionError that might occur.

By catching the ZeroDivisionError specifically, instead of catching all exceptions in one go, you can make sure that your users receive the right message that responds to the action they took. The message will only print when they enter a 0 as their second input.

But what about the initial problem? What if they enter something that isn't a number at all? In the next lesson, you'll learn how you can use multiple except blocks to catch more than just one exception at a time.