Error Types
Syntax Errors
A syntax error is a type of mistake that occurs when writing or interpreting code. It violates the rules of the programming language, causing the program to fail during compilation or execution.
Example
print("Hello, world!)Common Errors
- Missing or mismatched parentheses, brackets, or quotes.
- Improper indentation or inconsistent use of tabs and spaces.
- Invalid variable names (e.g., starting with a number, containing spaces).
- Using reserved keywords as variable names.
- Incorrect or missing punctuation, such as colons or commas.
- Missing or extra operators (e.g., == vs = for comparison and assignment).
# What error type will this code result in?
name = input('What's your name?')
if name == 'bob':
print('Nice to meet you, bob')
Runtime errors
Runtime errors occur during the execution of a program (while it is running, hence the phrase runtime). These errors cause the program to terminate abnormally.
Example
mylist = [1,2,3,4]
for i in range(len(mylist)):
print(mylist.pop(i))
Common runtime errors include
- Division by zero (ZeroDivisionError).
- Accessing an index that's out of range (IndexError).
- Trying to convert an invalid string to a number (ValueError).
- Accessing a non-existent key in a dictionary (KeyError).
- Opening a file that doesn't exist (FileNotFoundError).
- Trying to perform an unsupported operation (e.g., appending to an integer).
What type of error occurs when you type input)) instead of input()
Logical Errors
Logical errors occur when the code runs without any syntax or runtime errors, but it does not produce the expected output or behavior. These errors arise due to flaws in the algorithm or incorrect logical reasoning.
Detecting and fixing logical errors can be challenging as the code technically functions correctly, but the desired outcome is not achieved.
Example
num1 = 10
num2 = 5
sum = num1 - num2 # Incorrect operation for addition
print("Sum:", sum)
Common errors
- Incorrect order of operations in mathematical calculations.
- Using the wrong operator (e.g., using + for subtraction).
- Off-by-one errors in loops or array indices.
- Incorrect handling of conditional statements.
- Misunderstanding boolean logic (e.g., using and when or is intended).
What is a logical error?
Independent Research Task
Objective
Investigate the most expensive programming errors in terms of
- Money â Financial damages/losses
- Liberty â Jail time or legal consequences
- Lives â Fatal consequences due to software failures our
Task
Find real-world examples of programming errors that fall into each of these categories:
- Syntax Errors (e.g., mistyped code that halted systems)
- Runtime Errors (e.g., crashes due to unhandled exceptions)
- Logical Errors (e.g., flawed algorithms that seemed correct but had disastrous outcomes)
Summarize each case
What happened? What was the error? What was the impact (money, liberty, deaths)? Could it have been prevented?
Error Types
- Syntax Errors
- Runtime errors
- Logical Errors
- Independent Research Task