Error Types#
You have probably encountered errors before in your code—no one is perfect! But have you ever spent time reading through the error message to help you figure out what went wrong? This Section will give you some information that will help interpret these sometimes cryptic message to more efficiently identify the problem and fix it.
Essentially there are 3 types of errors, each of which is described below:
Syntax errors
Exceptions
Logical errors
Click –> Live Code on the top right corner of this screen and then execute the cells as you go through the page. In each case, see if you can fix the code (re-run the cell until the error is gone), as well as try to replicate the error with your own example.
In each of the examples, you should look at the error report that is printed after executing each cell and identify the type of error; in each case they should look like XxxxError
, where Xxxx
identifies the specific error type.
The work on this page is derived from work that is Copyright © University of Cape Town under CC BY-SA 4.0. Specifically, material from this page was used.
Syntax errors#
Syntax errors are the most common for beginner developers. When an error happenes, Python parser will show in which line it is and point to it in the code with an arrow ^
. Run the code snippet below for an example:
if 5 > 3:
print("5 is bigger than 3")
else
print("3 is bigger than 5")
Cell In[2], line 3
else
^
SyntaxError: invalid syntax
As we can see from the error report, Python tells us directly that this is a SyntaxError
. It even includes a carat (the ^
symbol) to identify the location within the line of code where it occurs. Useful, right?!
Exceptions#
Exceptions is a general set of errors that, unlike syntax errors, appear during code execution. If an exception stops your code from running, we often refer to this as raising an exception. Many exceptions are implemented directly in the Python code base, but it is important to recognize that anyone writing Python code can create exceptions themselves. Determining how and when to raise exceptions (i.e., “cause” the error) during code execution is a useful way to make sure code and software runs as designed; we will learn to do this later. For now we will look at several examples of exceptions that are implemented in the Python code base:
16 / 0
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[2], line 1
----> 1 16 / 0
ZeroDivisionError: division by zero
2 + "3"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 2 + "3"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
2 * pi * r ** 2
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 1
----> 1 2 * pi * r ** 2
NameError: name 'pi' is not defined
As you see in the examples above, we have encountered 3 different types of exceptions - ZeroDivisionError
, TypeError
and NameError
. A nice thing about Python is that it tells us the specific type of exception we are dealing it, making it easy for you, the developer, to figure out the cause of the raised exceptions. Although in some cases the cause of the exception is obvious, it is often useful to look up the Python documentation that describes it (especially when your code gets complex, and you are having trouble identifying the source of the problem). For example, if you look at the description of NameError
, you will find:
exception NameError
Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.The name attribute can be set using a keyword-only argument to the constructor. When set it represent the name of the variable that was attempted to be accessed.
Changed in version 3.10: Added the
name
attribute.
As you can see, the documentation also provides information about changes relative to older versions of Python.
For more exception types, you can have a look at the complete list here. Keep in mind that you are not required to understand the cause of every exception, but rather the importance of having different types of exceptions and that each one may require a different strategy in resolving it.
Logical errors#
Logical errors are generally the most difficult to discover, because they can be invisible to the developer. Logical errors can exist without raising an exception or an error. Particular examples of logical errors are:
using the wrong variable name
indenting a block to the wrong level
using integer division instead of floating-point division
getting operator precedence wrong
making a mistake in a boolean expression
forgetting to add an
else
statement to anif
clauseoff-by-one, and other numerical errors
Here is a brief example of using integer division instead of floating-point division:
Consider you have to compute speed from distance and time using the basic formula \(speed=\frac{distance}{time}\). Here is the correct version, using floating-point division:
distance = 10 # m
time = 3.63 # s
speed = distance / time # m/s
print('Speed = ', speed)
Speed = 2.7548209366391188
If we use integer division by mistake, the result is incorrect:
distance = 10 # m
time = 3.63 # s
speed = distance // time # m/s
print('Speed = ', speed)
Speed = 2.0
The difference between the 2 results is nearly 0.75, which can be very high depending on its use.
Summary of Error Types#
On this page we considered three types of errors. One way to help distinguish them is to recognize when they may occur:
Syntax error: before the code is executed
Exceptions: during execution
Logic error: code executes without error
When executing the code in the examples above, did you read the error report? It is called a traceback and it should have been useful to identify the error type. We will learn more about it in the next section.