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.
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:
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):
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:
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
In this tree you will find all exception that Pyerrors can handle:
|- • 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.
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.