In this Python Tutorial let us learn about the 3 different pieces of information that you can extract and use from the Exceptions caught on your except clauses, and see the best ways to use each of these pieces in our Python programs.
Let us start by learning what the 3 pieces of information are.
What kind of information can you get from Exceptions?
You can get the following 3 pieces of data from exceptions
- Exception Type,
- Exception Value a.k.a Error Message, and
- Stack-trace or Traceback Object.
All three of the above information is printed out by the Python Interpreter when our program crashes due to an exception as shown in the following example
>> my_list = [1,2] >> print (my_list) Traceback (most recent call last): File "<ipython-input-35-63c7f9106be5>", line 1, in <module> print (my_list) IndexError: list index out of range
Lines 3,4,5,6 shows the Stack-trace
Line 7 shows the Exception type and Error Message.
Our focus in this article is to learn how to extract the above 3 pieces individually in our except clauses and print them out as needed.
Hence, the rest of the article is all about answering the following questions
- what does each of the information in the above list mean,
- how to extract each of these 3 pieces individually and
- how to use these pieces in our programs.
Piece#1: Printing Exception Type
The Exception Type refers to the class to which the Exception that you have just caught belongs to.
Extracting Piece#1 (Exception Type)
Let us improve our Example 1 above by putting the problematic code into try and except clauses.
try: my_list = [1,2] print (my_list) except Exception as e: print(type(e))
Here, in the try clause, we have declared a List named my_list and initialized the list with 2 items. Then we have tried to print the 3rd/non-existent item in the list.
The except clause catches the IndexError exception and prints out Exception type.
On running the code, we will get the following output
As you can see we just extracted and printed out the information about the class to which the exception that we have just caught belongs to!
But how exactly did we do that?
If you have a look at the except clause. In the line
except Exception as e:
what we have done is, we have assigned the caught exception to an object named “e”. Then by using the built-in python function type(), we have printed out the class name that the object e belongs to.
Where to get more details about Exception Types
Now that we have the “Exception Type”, next we will probably need to get some information about that particular type so that we can get a better understanding of why our code has failed. In order to do that, the best place to start is the official documentation.
For built in exceptions you can have a look at the Python Documentation
For Exception types that come with the libraries that you use with your code, refer to the documentation of your library.
Piece#2: Printing Exception Value a.k.a Error message
The Exception type is definitely useful for debugging, but, a message like IndexError might sound cryptic and a good understandable error-message will make our job of debugging easier without having to look at the documentation.
In other words, if your program is to be run on the command line and you wish to log why the program just crashed then it is better to use an “Error message” rather than an “Exception Type”.
The example below shows how to print such an Error message.
try: my_list = [1,2] print (my_list) except Exception as e: print(e)
This will print the default error message as follows
list index out of range
Each Exception type comes with its own error message. This can be retrieved using the built-in function print().
Say your program is going to be run by a not-so-tech-savvy user, in that case, you might want to print something friendlier. You can do so by passing in the string to be printed along with the constructor as follows.
try: raise IndexError('Custom message about IndexError') except Exception as e: print(e)
This will print
Custom message about IndexError
To understand how the built-in function print() does this magic, and see some more examples of manipulating these error messages, I recommend reading my other article in the link below.
If you wish to print both the Error message and the Exception type, which I recommend, you can do so like below.
try: my_list = [1,2] print (my_list) except Exception as e: print(repr(e))
This will print something like
IndexError('list index out of range')
Now that we have understood how to get control over the usage of Pieces 1 and 2, let us go ahead and look at the last and most important piece for debugging, the stack-trace which tells us where exactly in our program have the Exception occurred.
Piece#3: Printing/Logging the stack-trace using the traceback object
Stack-trace in Python is packed into an object named traceback object.
This is an interesting one as the traceback class in Python comes with several useful methods to exercise complete control over what is printed.
Let us see how to use these options using some examples!
import traceback try: my_list = [1,2] print (my_list) except Exception: traceback.print_exc()
This will print something like
Traceback (most recent call last): File "<ipython-input-38-f9a1ee2cf77a>", line 5, in <module> print (my_list) IndexError: list index out of range
which contains the entire error messages printed by the Python interpreter if we fail to handle the exception.
Here, instead of crashing the program, we have printed this entire message using our exception handler with the help of the print_exc() method of the traceback class.
The above Example-6 is too simple, as, in the real-world, we will normally have several nested function calls which will result in a deeper stack. Let us see an example of how to control the output in such situations.
def func3(): my_list = [1,2] print (my_list) def func2(): print('calling func3') func3() def func1(): print('calling func2') func2() try: print('calling func1') func1() except Exception as e: traceback.print_exc()
Here in the try clause we call func1(), which in-turn calls func2(), which in-turn calls func3(), which produces an IndexError. Running the code above we get the following output
calling func1 calling func2 calling func3 Traceback (most recent call last): File "<ipython-input-42-2267707e164f>", line 15, in <module> func1() File "<ipython-input-42-2267707e164f>", line 11, in func1 func2() File "<ipython-input-42-2267707e164f>", line 7, in func2 func3() File "<ipython-input-42-2267707e164f>", line 3, in func3 print (my_list) IndexError: list index out of range
Say we are not interested in some of the above information. Say we just want to print out the Traceback and skip the error message and Exception type (the last line above), then we can modify the code like shown below.
def func3(): my_list = [1,2] print (my_list) def func2(): func3() def func1(): func2() try: func1() except Exception as e: traceback_lines = traceback.format_exc().splitlines() for line in traceback_lines: if line != traceback_lines[-1]: print(line)
Here we have used the format_exc() method available in the traceback class to get the traceback information as a string and used splitlines() method to transform the string into a list of lines and stored that in a list object named traceback_lines
Then with the help of a simple for loop we have skipped printing the last line with index of -1 to get an output like below
Traceback (most recent call last): File "<ipython-input-43-aff649563444>", line 3, in <module> func1() File "<ipython-input-42-2267707e164f>", line 11, in func1 func2() File "<ipython-input-42-2267707e164f>", line 7, in func2 func3() File "<ipython-input-42-2267707e164f>", line 3, in func3 print (my_list)
Another interesting variant of formatting the information in the traceback object is to control the depth of stack that is actually printed out.
If your program uses lots of external library code, odds are the stack will get very deep, and printing out each and every level of function call might not be very useful. If you ever find yourself in such a situation you can set the limit argument in the print_exc() method like shown below.
This will limit the number of levels to 2. Let us use this line of code in our Example and see how that behaves
def func3(): my_list = [1,2] print (my_list) def func2(): func3() def func1(): func2() try: func1() except Exception as e: traceback.print_exc(limit=2)
This will print
Traceback (most recent call last): File "<ipython-input-44-496132ff4faa>", line 12, in <module> func1() File "<ipython-input-44-496132ff4faa>", line 9, in func1 func2() IndexError: list index out of range
As you can see, we have printed only 2 levels of the stack and skipped the 3rd one, just as we wanted!
You can do more things with traceback like formatting the output to suit your needs. If you are interested to learn even more things to do, refer to the official documentation on traceback here
Now that we have seen how to exercise control over what gets printed and how to format them, next let us have a look at some best practices on when to use which piece of information
Best Practices while Printing Exception messages
When to Use Which Piece
- Use Piece#1 only on very short programs and only during the development/testing phase to get some clues on the Exceptions without letting the interpreter crash your program. Once finding out, implement specific handlers to do something about these exceptions. If you are not sure how to handle the exceptions have a look at my other article below where I have explained 3 ways to handle Exceptions
Exceptions in Python: Everything You Need To Know!
- Use Piece#2 to print out some friendly information either for yourself or for your user to inform them what exactly is happening.
- Use all 3 pieces on your finished product, so that if an exception ever occurs while your program is running on your client’s computer, you can log the errors and have use that information to fix your bugs.
Where to print
One point worth noting here is that the default file that print() uses is the stdout file stream and not the stderr stream. To use stderr instead, you can modify the code like this
import sys try: #some naughty statements that irritate us with exceptions except Exception as e: print(e, file=sys.stderr)
The above code is considered better practice, as errors are meant to go to stderr and not stdout.
You can always print these into a separate log file too if that is what you need. This way, you can organize the logs collected in a better manner by separating the informational printouts from the error printouts.
How to print into log files
If you are going to use a log file I suggest using python’s logging module instead of print() statements, as described here
If you are interested in learning how to manually raise exceptions, and what situations you might need to do that you can read this article below
If you are interested in catching and handling multiple exception in a single except clause, you can this article below
And with that, I will conclude this article!
I hope you enjoyed reading this article and got some value out of it!
Feel free to share it with your friends and colleagues!