A compile-time error is detected before the program runs because the code breaks a language rule. A runtime error occurs during execution, caused by an unexpected condition such as dividing by zero. Both error types appear in GCSE exams, and each requires a different debugging strategy.

What is a compile-time error (syntax error)?

A compile-time error — more commonly called a syntax error in Python — occurs when the Python interpreter reads the code and finds that it breaks the rules of the language. The program cannot start at all.

Python is an interpreted language: it reads your code and converts it to bytecode before running. If the code is grammatically incorrect, this translation step fails and Python reports a SyntaxError before executing a single line.

Common causes:

Cause Broken code Error message
Missing colon def greet() SyntaxError: expected ':'
Mismatched brackets print("Hello" SyntaxError: '(' was never closed
Misspelt keyword whle x > 0: SyntaxError: invalid syntax
Wrong indentation print("hi") not indented after if IndentationError
Missing quotes name = hello SyntaxError or NameError depending on context

Syntax errors are the easiest type to fix because Python points directly to the line containing the problem (though occasionally the real error is on the line before the reported line).

What is a runtime error?

A runtime error occurs while the program is actually running — the code was syntactically correct and started executing, but encountered an unexpected condition that it cannot handle.

Python raises an exception and halts the program, printing a traceback that shows exactly which line caused the problem. Common runtime exceptions:

Exception Typical cause Example
ZeroDivisionError Division by zero 10 / 0
NameError Using a variable before assigning it print(total) before total = 0
TypeError Operation on incompatible types "5" + 3 (str + int)
IndexError Accessing an index outside the list my_list[10] on a list of 5 items
ValueError Function receives wrong value type int("hello")
FileNotFoundError Opening a file that doesn't exist open("missing.txt")

Runtime errors cannot be caught by the interpreter before execution — they depend on data (the values at runtime), not just structure (the grammar of the code).

How do compile-time and runtime errors compare?

Feature Compile-time (syntax) error Runtime error
When detected Before any code runs During execution
Can program start? No Yes — until the error line is reached
Cause Code breaks language grammar rules Unexpected data condition at runtime
Python signal SyntaxError, IndentationError Exception: ZeroDivisionError, NameError, etc.
How to spot IDE highlights immediately; program won't run Program runs, then crashes on a specific line
Difficulty to fix Usually easy — Python points to the line Can be harder — depends on live data

A useful analogy: a compile-time error is like a grammatical mistake in an essay that makes the sentence impossible to parse. A runtime error is like a valid sentence with a factual error — "divide 10 by 0" is grammatically correct English but meaningless in practice.

What is a logic error?

Beyond compile-time and runtime errors, there is a third category: logic errors. The program runs without crashing but produces the wrong result. Logic errors are the hardest to find because no error message is generated.

# Logic error: wrong formula for area of a circle
import math
radius = 5
area = math.pi * radius   # missing ** 2 — gives 15.7 instead of 78.5
print(area)               # 15.707... — no crash, but wrong

Fixing logic errors requires careful testing against expected values and working through the logic on paper.

How do you debug a runtime error in Python?

A Python traceback (error message) reads from bottom to top:

Traceback (most recent call last):
  File "quiz.py", line 14, in <module>
    answer = int(user_input)
ValueError: invalid literal for int() with base 10: 'five'

Reading strategy:

  1. Last line — the error type (ValueError) and its message (invalid literal for int()...).
  2. Line above last — the exact line of code that caused it (answer = int(user_input)).
  3. File and line numberquiz.py, line 14.

Fix: add input validation before converting:

user_input = input("Enter a number: ")
if user_input.isdigit():
    answer = int(user_input)
else:
    print("Please enter a valid number.")

Frequently asked questions

Python is an interpreted language — why does it still have "compile-time" errors?

Strictly speaking, Python does compile your code — to bytecode — before executing it. The SyntaxError is thrown during this compilation phase. The term "compile-time error" is therefore accurate for Python, even though Python is often called "interpreted". The practical effect is the same as in a traditional compiled language: a syntactically incorrect program never runs at all.

Can a runtime error be prevented without changing the logic?

Yes — using exception handling (try/except in Python). Wrapping potentially failing code in a try block catches the exception and handles it gracefully rather than crashing:

try:
    result = 10 / int(input("Enter a divisor: "))
    print(result)
except ZeroDivisionError:
    print("Cannot divide by zero.")
except ValueError:
    print("Please enter a number.")

Exception handling is a key concept at GCSE — it is the standard way to protect against predictable runtime errors such as invalid user input or missing files.

Why does Python sometimes show the wrong line number in a SyntaxError?

Python's parser reads ahead when checking syntax. A missing closing bracket on line 3 may not be detected until the parser reaches line 4 and finds something unexpected. This is why fixing the reported line sometimes does not fix the error — look at the preceding line too, especially for unclosed brackets, parentheses, or quotation marks.

What is the difference between an exception and an error?

In Python, all errors are exceptions — they are objects belonging to classes in the Exception hierarchy. SyntaxError, RuntimeError, ZeroDivisionError, and NameError are all exceptions. The term "error" is informal and refers to any problem that stops the program. Some exceptions are recoverable (caught by try/except); others (like SystemExit and KeyboardInterrupt) usually should not be caught. At GCSE, the distinction is rarely tested — understanding when errors occur and how to handle them is what matters.


Diagnose and fix Python errors with guided debugging practice from Professor Turing at aitutors.me.