1.8. Debugging#
Syntax Error, Runtime Error, and Semantic Error are the three main types of errors encountered in programming. Let’s define each of them:
1.8.1. Syntax Error#
A syntax error occurs when the code violates the rules of the programming language’s syntax. These errors are caught by the Python interpreter during the parsing phase before the program is executed. Syntax errors are typically caused by missing or misplaced characters, incorrect indentation, or improper use of keywords and symbols [Downey, 2015, Python Software Foundation, 2024].
Example of a syntax error:
# Missing a colon after the 'if' statement
if x > 5
print("x is greater than 5")
Output:
File "<ipython-input-2-b98671540bfa>", line 2 if x > 5 ^ SyntaxError: expected ':'
1.8.2. Runtime Error#
A runtime error (also known as an exception or an error) occurs during the execution of the program when something unexpected happens. These errors are not caught during the parsing phase but arise during the program’s runtime. Runtime errors can be caused by various factors, such as division by zero, accessing an out-of-bounds index in a list, or calling a function that does not exist [Downey, 2015, Python Software Foundation, 2024].
Example of a runtime error:
x = 5
y = 0
result = x / y # This will raise a ZeroDivisionError
Output:
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
Cell In[20], line 3
1 x = 5
2 y = 0
----> 3 result = x / y
ZeroDivisionError: division by zero
1.8.3. Semantic Error#
A semantic error, also known as a logic error, occurs when the code is syntactically correct and runs without any errors, but it does not produce the expected or desired results due to incorrect logic or reasoning. These errors can be challenging to identify, as the program runs without any error messages. Semantic errors often lead to unexpected behavior or incorrect output [Downey, 2015, Python Software Foundation, 2024].
Example of a semantic error:
'''
The purpose of this function is to compute the average of a list of numbers
and present the result as an integer value.
'''
def calculate_average(numbers):
total = 0
for num in numbers:
total = total + num
average = total / len(numbers)
return average
numbers_list = [5, 10, 15, 20]
result = calculate_average(numbers_list)
print(result)
# The above function will return 12.5, but it's not the correct average.
# The error is due to using 'total / len(numbers)' instead of 'total // len(numbers)'.
12.5
Summary
In summary,
Syntax errors are caught during the parsing phase before the program starts running.
Runtime errors occur during program execution when something unexpected happens, and they raise exceptions.
Semantic errors lead to incorrect behavior or output, but the code runs without any error messages. Identifying and fixing these errors is an essential part of the development process.
1.8.4. Exception Handling (try, except, finally)#
Exception handling statements manage errors and exceptions gracefully.
Example:
try:
result = 10 / 0
except ZeroDivisionError:
print("Error: Division by zero")
finally:
print("This block always executes")
Error: Division by zero
This block always executes
In this example, a ZeroDivisionError
is caught, and the except
block is executed, followed by the finally
block.