Analytics Vidhya
Published in

Analytics Vidhya

Python Errors, How Can You Make Python Exceptions More Informative?

How to make python errors (python exceptions) more descriptive, easy to read and to understand.

python exception errors syntaxerror
Normal Python Syntax Error (Exception)

Errors or Exceptions are the most frustrated thing every programmer will encounter when working on a project. And python doesn’t make an exception, in fact I find python errors sometimes complex due to unclarity of some errors.

In the image above the error type is syntax error but the error message is so complex that no beginner (or sometimes intermediate) in python can understand, this error caused because I didn’t close a parenthesis in print statement:

print("Hello World!"  → # No closing parenthesis

It would be much easier if the interpreter can tell you to just look for typo or unclosed parenthesis or anything that can generate a syntax error in python script, luckily I’ve created a script called Pyerrors that can do this exact job for you, check the image down below:

python errors pyton exception
Pyerrors Script In Action

But what is really happened under the hood? I’ve collected every possible exception and I have created a handler that can take as much information as possible from the error and reformat the error message so that it can be as easy as possible for the reader to understand.

This is not everything, actually this is a basic and easy to solve type of exceptions, what about let’s say IndexError?

lst = []
for i in range(10):
lst.append(i)
print(lst[100])

This will trigger an IndexError the message will be out of range, this code is simple but what if you’re using nested loops or long while loop statement, this error specifically caused me to re-write my entire codes when I was trying to work on Algorithm project. Now let’s see what can Pyerrors do with this type of errors:

Pyerrors IndexError

The script return a simple error indicating the line number and info of what to do, in this case the script is telling you that the index at line 2 does not exist. This is a lot simpler and easy to understand than the normal python exception message.

How To Use?

If you’re interested in tying the script, first you need to download the source code from this LINK, next then you specify an argument and path.

>>> python pyerrors.py --errors|-e path_to_your_script

And that’s all to get started. But please note that Pyerrors may use modules that are not installed in your system, all you need to do is this:

1 - Navigate to Pyerrors folder from your terminal
2 - run this code: pip install -r requirements.txt

Also, Pyerrors have the ability to search for warnings in your script, usually python warnings are triggered by the user so if you have very big code and you want to check whether a certain parameter will trigger a warning you may want to use this feature:

python pyerrors.py --warnings|-w path_to_your_script

Exception Tree:

In this tree you will find all exception that Pyerrors can handle:

Exeptions:
|
|- • AssertionError
|- • AttributeError
|- • EOFError
|- • FileNotFoundError
|- • ImportError
|- • IndexError
|- • KeyError
|- • KeyboardInterrupt
|- • MemoryError
|- • NameError
|- • NotImplementedError
|- • ModuleNotFoundError
|- • OSError
|- • OverflowError
|- • ReferenceError
|- • RuntimeError
|- • StopIteration
|- • SyntaxError
|- • IndentationError
|- • TabError
|- • SystemError
|- • TypeError
|- • UnboundLocalError
|- • ValueError
|- • ZeroDivisionError

And including every type of warnings in python.

Conclusion:

This is version 1.0, so it’s not a “mature” script it may have problems or bugs but in the future this script will have more powerful features and functionalities. If you like it or want to contribute to the script you’re welcome and don’t forget to leave a star this would help motivate me.

My Twitter

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

Git In Five Minutes

Making it easier for new co-ops to get their Articles of Association

In Defence of “Serverless” —the term

Greedy Algorithms — Djkastra Algorithm

Bluehost Review: Is It The Best Choice for Your Site?

SpaceBot: Leveraging Slack in Our Deployment Process

Using Reloader for Configuration rollout in Kubernetes

How to push into Github from Google Colab?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Riade

Riade

Hi!, my name is Riade, and I am python programmer, full stack web developer and intermediate data scientist.

More from Medium

Advanced Concepts in Python — II

Common Operations with Numbers, Strings and Lists in Python

Learn about Recursion Using Python

Object and Class Attributes in Python Object-Oriented Programming