Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Intermediate30 min
Types of errors hero Module 15 Lesson 2 compile logical runtime

Core idea: Every bug is not an exception. Python has 3 major error categories, but only runtime errors become exceptions that try/except can 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/except works only for runtime errors

The big picture

Think of building a Python program like driving a car.

Driving analogy compile car wont start logical wrong route runtime flat tire

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

python
print("Hello"

Immediately Python says:

text
SyntaxError: '(' was never closed

Why? The closing parenthesis is missing.

Visual explanation

Compile time syntax check program stops no execution begins

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

python
def discount_total(price, discount):
    return price + discount

print(discount_total(100, 10))

Output:

text
110

Is the program correct? No. Expected answer: 90. The formula is wrong.

Visual comparison

Logical error program runs expected 90 actual 110 wrong formula

Characteristics

Property Value
Happens During execution
Cause Wrong algorithm
Example Wrong formula
Fixed by Testing and debugging
try/except ❌ No

Important: try/except cannot 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

python
age = int("abc")

Python raises: ValueError

Example 2 — Division

python
10 / 0

Python raises: ZeroDivisionError

Without exception handling

python
result = 10 / 0
print(result)

Program terminates with a traceback.

With exception handling

python
try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero")

Output:

text
Cannot divide by zero

Why only runtime errors use try/except?

This is the most important concept in this lesson.

Program starts user abc input exception raised try except friendly message

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

Student marks compile logical runtime error examples

Compile-time:

python
print("Marks"

Program never opens.

Logical:

python
average = total  # forgot to divide by count

Student receives wrong grade.

Runtime:

python
marks = int("ninety")

Result: ValueError — use exception handling.

Example 2 — ATM machine

ATM compile logical runtime customer abc amount
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:

python
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

text
30
Cannot divide by zero

Notice: first line = wrong logic · second line = runtime exception handled successfully.


Memory map

Memory map three types compile logical runtime try except

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