So you can handle user errors and server errors appropriately

Halil Yıldırım
Jun 9 · 3 min read
Photo by Jan Genge on Unsplash

Imagine that you developed a calculator using Python. Someone who uses your software wants to divide a number by zero (e.g. 4/0). Because division by zero is mathematically undefined, Python is going to throw an error like below.

ZeroDivisionError: division by zero

It wouldn’t be nice to show an error like this. For these cases, we can use Try/Except Blocks.

I will give you a very basic example of this to give you a good understanding of how you can use it.

try:
print(4/0)
except ZeroDivisionError:
print("Division by zero is undefined")

The output of this code is going to be:

Division by zero is undefined

Let’s give another example of an error to catch and handle. For example, as you can’t add integers to strings, you will get a TypeError when you execute this code below.

print(12+"string")

TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

What can we do to avoid it? Just as simple as below:

try:
print(12+"string")
except TypeError:
print("You cannot add integers to strings.")

The user now will face an error message “You cannot add integers to strings.”

Okay, what if you don’t want to throw a custom error message? It is as simple as writing this code:

try:
print(12+"string")
except TypeError as e:
print(e)

Output of this code above:

unsupported operand type(s) for +: ‘int’ and ‘str’

As you’ve seen in examples given above, I used except TypeError and ZeroDivisionError. Actually, you can use Exception in both cases.

try:
print(12+"string")
except Exception:
print("You cannot add integers to strings.")

It will work as well. But it would be nice if you specify which exceptions you are trying to catch. I mean, your goal is to catch and handle a type error; it could be better to use except TypeError instead of except Exception.

Okay, it is good, but what if we are expecting two errors? What I mean is:

try:
print(4/0)
print(12+"string")
except Exception:
print("This is an error.")

It will throw an error like “This is an error.” except Exception catches both TypeError and ZeroDivisionError. So it works. But I’ve just said that it is good to specify the error instead of using a general Exception. But which one am I going to use, except TypeError or except ZeroDivisionError? Both TypeError and ZeroDivisionError exist in this code just above. We will do something like:

try:
print(4/0)
print(12+"string")
except (TypeError, ZeroDivisionError):
print("This an error.")

Custom Exceptions

You may want to use your own custom exceptions. You can do it like this:

try:
x = 'something'
if x == 'something':
raise Exception
except Exception
:
print("x is something")

Output of this code is:

x is something

In this example, I defined a string called x. And I just wanted to throw an error if x is something. This is just an example; you may use it for another case when needed.

And Finally

In both cases, whether you catch an error or not, this will execute.

try:
print(4/0)
except ZeroDivisionError as e:
print(e)
finally:
print("I will always execute")

We caught a ZeroDivisionError. And the output is:

division by zero
I will always execute

try:
print(4/1)
except ZeroDivisionError as e:
print(e)
finally:
print("I will always execute")

We didn’t catch any ZeroDivisionError as it is okay to divide 4 by 1. The output is:

4.0
I will always execute

As it is clear that finally works in both cases.

Thank you for reading.

Better Programming

Advice for programmers.

Halil Yıldırım

Written by

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade