Tuesday, April 4, 2017

Reading Python error messages

What every beginner needs to know

It's normal to make mistakes when you first start learning to program. Python uses exceptions to report errors. Usually, your program will just print an error message and abort. These error messages contain a lot of information, but you have to know how to read them.


Here’s a sample error message




The message has 2 parts
  1. A traceback, showing the path taken by the program when the error occurred.
  2. The name of the exception followed by a short description.


The traceback is important because it shows the exact line of code containing the error. In this case, we can see the problem is in line 9 of the “C” method. We can also see how we got there
  • method A (line 5), which called
  • method B (line 7), which called
  • method C (line 9), where the error occurred

The last line of the message reads “ZeroDivisionError: float division by zero”. This lets us know that dividing by zero is not allowed and we need to fix it.

So don’t panic the next time one of your programs fails. Read the error message carefully and you can probably figure out what’s wrong.

No comments:

Post a Comment