1.2.5 Types of error

Types of Programming Error

Three types of error can occur when writing programs. Understanding them is essential for debugging effectively — each requires a different approach to find and fix.

Error typeWhen it occursDoes the program run?
Syntax errorDuring translation (before execution)No — program will not run at all
Runtime errorDuring executionStarts, then crashes
Logic errorDuring executionRuns to completion but gives wrong results

A syntax error is a violation of the grammar rules of the programming language. The interpreter cannot understand the code and refuses to run it. Syntax errors are reported when you attempt to run the program.

Common causes: missing colons, incorrect indentation, misspelled keywords, mismatched brackets.

# Syntax error — missing colon after if
if score > 50
    print("Pass")

# Syntax error — misspelled keyword
pront("Hello")

# Syntax error — mismatched bracket
result = (3 + 4 * 2

✓ Fix: read the error message carefully — Python indicates the line number and often the position of the error.

A runtime error occurs whilst the program is running — the syntax is valid so the program starts, but something goes wrong during execution and it crashes. Runtime errors produce an error message called an exception.

Common causes: dividing by zero, accessing an index that does not exist, using a variable before assigning it a value, trying to convert non-numeric input to an integer.

# Runtime error — division by zero
x = 10
y = 0
print(x / y)           # ZeroDivisionError

# Runtime error — index out of range
nums = [1, 2, 3]
print(nums[5])         # IndexError: list index out of range

# Runtime error — invalid conversion
age = int("hello")     # ValueError

✓ Fix: add checks (e.g. guard against zero before dividing; validate input before converting).

A logic error is the most dangerous type — the program runs without crashing but produces incorrect results. The code is syntactically valid and executes without exceptions, but the algorithm is wrong. There is no error message to guide you.

Common causes: wrong operator (> instead of >=), incorrect loop range, wrong variable updated, conditions in wrong order.

# Intended: calculate the average of 5 numbers
total = 0
for i in range(1, 5):    # BUG: range(1,5) gives 4 numbers, not 5
    total = total + i
average = total / 5      # divides by 5 but only added 4 values
print(average)           # prints 2.0 instead of 3.0

# Fix: change range(1, 5) to range(1, 6)

✓ Fix: use trace tables, test with known inputs, add print statements to inspect variable values at key points.

 Key Takeaways

  • Syntax error: breaks grammar rules — program will not run; detected immediately by the interpreter.
  • Runtime error: occurs during execution — program crashes and raises an exception.
  • Logic error: program runs but produces wrong output — hardest to find as there is no error message.
  • Logic errors are identified using trace tables, test data and systematic debugging.