Lesson 02
Types of Errors in Python — Compile, Logical, and Runtime
Identify compile-time, logical, and runtime errors in Python — and learn why only runtime errors are handled with try and except.
Core idea: Every bug is not an exception. Python has 3 major error categories, but only runtime errors become exceptions that
try/exceptcan handle.
Learning objectives
By the end of this lesson, you will be able to:
- Identify compile-time, logical, and runtime errors
- Recognize when each error occurs
- Understand who fixes each error
- Know why
try/exceptworks only for runtime errors
The big picture
Think of building a Python program like driving a car.
One-line memory trick
| Error | Real life |
|---|---|
| Compile-time | Car will not start |
| Logical | Wrong destination |
| Runtime | Accident while driving |
1. Compile-time errors
What is a compile-time error?
A compile-time error happens before the program starts running. Python first checks whether your code follows its grammar rules (syntax). If the syntax is wrong, the program never starts.
Example
print("Hello"Immediately Python says:
SyntaxError: '(' was never closedWhy? The closing parenthesis is missing.
Visual explanation
Characteristics
| Property | Value |
|---|---|
| Happens | Before execution |
| Cause | Syntax mistake |
| Example | Missing ) or : |
| Fixed by | Programmer |
| try/except | ❌ No |
2. Logical errors
What is a logical error?
The program runs successfully, but the answer is wrong.
No crash. No exception. Just incorrect logic.
Example
def discount_total(price, discount):
return price + discount
print(discount_total(100, 10))Output:
110Is the program correct? No. Expected answer: 90. The formula is wrong.
Visual comparison
Characteristics
| Property | Value |
|---|---|
| Happens | During execution |
| Cause | Wrong algorithm |
| Example | Wrong formula |
| Fixed by | Testing and debugging |
| try/except | ❌ No |
Important:
try/exceptcannot detect wrong mathematics. It only catches exceptions.
3. Runtime errors (Exceptions)
What is a runtime error?
A runtime error happens while the program is running because of an impossible or invalid operation.
Examples:
- User enters letters instead of numbers
- Divide by zero
- File does not exist
- Invalid list index
These become exceptions.
Example 1 — Invalid input
age = int("abc")Python raises: ValueError
Example 2 — Division
10 / 0Python raises: ZeroDivisionError
Without exception handling
result = 10 / 0
print(result)Program terminates with a traceback.
With exception handling
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")Output:
Cannot divide by zeroWhy only runtime errors use try/except?
This is the most important concept in this lesson.
Why not compile-time?
Because the program never starts — syntax is checked before runtime.
Why not logical?
Because nothing technically goes wrong. Python thinks your logic is valid — you just get the wrong answer.
Side-by-side comparison
| Feature | Compile-time | Logical | Runtime |
|---|---|---|---|
| Program starts? | ❌ No | ✅ Yes | ✅ Yes |
| Program crashes? | Yes (before run) | No | Yes (without handling) |
| Wrong result? | No | ✅ Yes | Maybe |
| Exception created? | ❌ No | ❌ No | ✅ Yes |
| Use try/except? | ❌ | ❌ | ✅ |
Real-world examples
Example 1 — Student marks system
Compile-time:
print("Marks"Program never opens.
Logical:
average = total # forgot to divide by countStudent receives wrong grade.
Runtime:
marks = int("ninety")Result: ValueError — use exception handling.
Example 2 — ATM machine
| Situation | Error type |
|---|---|
Missing : in source code |
Compile-time |
| Interest formula incorrect | Logical |
Customer enters "abc" amount |
Runtime |
Only the third case uses exception handling.
Interactive playground
Predict the output before running the playground:
def avgop(m, p):
return m + p
print(avgop(10, 20))
try:
print(10 / 0)
except ZeroDivisionError:
print("Cannot divide by zero")Step-by-step execution
| Step | What happens |
|---|---|
| 1 | avgop(10, 20) returns 30 |
| 2 | This is a logical error — average should be 1.5, not 30 |
| 3 | 10/0 raises ZeroDivisionError |
| 4 | except catches it and prints a friendly message |
Final output
30
Cannot divide by zeroNotice: first line = wrong logic · second line = runtime exception handled successfully.
Memory map
Key vocabulary
| Term | Meaning |
|---|---|
| Compile-time error | Syntax mistake detected before execution |
| SyntaxError | Invalid Python grammar |
| Logical error | Correct syntax but wrong algorithm |
| Runtime error | Error occurring during execution |
| Exception | Runtime error represented as an object |
| Exception handling | Using try and except to recover gracefully |
Interview questions
Q1. Which error is handled using try/except?
Answer: Runtime errors (exceptions).
Q2. Why cannot SyntaxError be caught by try/except?
Answer: Because the program never starts executing — syntax is checked before runtime.
Q3. What is the difference between logical and runtime errors?
Answer: A logical error produces an incorrect result without raising an exception; a runtime error raises an exception during execution.
Key takeaways
- Compile-time → syntax mistakes before execution
- Logical → program runs but produces incorrect results
- Runtime → exceptions from invalid input or impossible operations
- Only runtime errors are handled using
try+except - Next: Built-in Exceptions — ValueError, TypeError, IndexError, KeyError, ZeroDivisionError, FileNotFoundError
Loading playground…
Check your understanding
Types of errors quiz
Compile-time errors occur:
1 / 10