8) a. Traceback

The most important thing that you will need to learn about errors in Python is to understand how and why they occur. Fortunately Python does a great job of providing you with the necessary information when that happens.

Try and run the following piece of code.

And you will see something like this as the output.

1
2
3
4
5
6
Traceback (most recent call last):
  File "C:/Users/USER/drafts.py", line 1, in <module>
    faulty_function()
  File "C:/Users/USER/drafts.py", line 2, in faulty_function
    return 1 / 0
ZeroDivisionError: division by zero

This is called a traceback. It allows you to trace the error back to its origin.

An easy way to read a traceback for a beginner is to focus on a few key areas that give you important information.

If you read it from top to bottom you can see that the error occurred when the faulty_function() which is in C:/Users/USER/drafts.py was called. More specifically it was the line 2 that had the problem.

Finally on the last line you can see the name of the error and an error message that provides more context.