Member-only story

Python: Exception handling

Tomas Svojanovsky
5 min readAug 10, 2023

--

Coding high-quality code without errors is the goal of every programmer. However, encountering errors during programming is inevitable. Errors can happen for various reasons, often when we’re learning something new. This includes syntax errors, incorrect method calls, passing wrong parameters, and more. Sometimes, errors occur simply due to oversight. Mistakes are a common part of the process.

In the applications where we get data from the users or from API, for example, forms, we don’t know what the user will type. In the happy path, everything works fine, but if we accept an integer, but the user will type some text it will lead to errors.

In applications where we receive data from users or APIs. Imagine a form, we cannot predict the exact input a user will provide. The difference between what we expect and the actual data we get can lead to errors.

It’s up to us to catch the error and provide feedback to the user on what happened. Otherwise, the application just fails and will lead to a weird situation. That’s not what we want.

To avoid this scenario we have a mechanism that can handle these situations — it’s called exception handling.

Photo by Nada Habashy on Unsplash

We have an exception tree with the most general exception at the top and the most specific at the bottom. Why is this important?

--

--

Tomas Svojanovsky
Tomas Svojanovsky

Written by Tomas Svojanovsky

I'm a full-stack developer. Programming isn't just my job but also my hobby. I like developing seamless user experiences and working on server-side complexities

No responses yet