Types of Errors

Not all errors are created equal. Understanding the different types helps you approach each one with the right strategy. Some errors announce themselves loudly; others hide in plain sight.

Syntax Errors

Syntax errors are mistakes in how you write code — like typos, missing punctuation, or incorrect indentation. These are the friendliest errors because your program won't even run until you fix them.

# Syntax error - missing closing parenthesis
print("Hello"

# Syntax error - missing colon
if x > 5
    print("Big number")

# Syntax error - wrong indentation
def greet():
print("Hi")  # Should be indented

Your code editor often highlights syntax errors before you run the code. The interpreter or compiler catches any that slip through and tells you exactly where the problem is.

Runtime Errors

Runtime errors happen when your code is syntactically correct but something goes wrong during execution. The program starts running, then crashes when it hits the problematic line.

# Runtime error - division by zero
result = 10 / 0  # ZeroDivisionError

# Runtime error - file doesn't exist
file = open("missing.txt")  # FileNotFoundError

# Runtime error - wrong type
length = len(42)  # TypeError: object of type 'int' has no len()

Runtime errors produce error messages with stack traces that show you where the crash occurred. These errors often depend on specific inputs or conditions — the same code might work fine with different data.

Logic Errors

Logic errors are the trickiest. Your code runs without crashing, but it produces the wrong result. No error message appears because technically nothing "went wrong" from the computer's perspective — it did exactly what you told it to do.

# Logic error - wrong formula
def average(a, b):
    return a + b / 2  # Should be (a + b) / 2

# Logic error - off-by-one
def get_last_item(items):
    return items[len(items)]  # Should be len(items) - 1

# Logic error - wrong condition
def is_adult(age):
    return age > 18  # Should be >= 18 for most definitions

Logic errors require careful testing to discover. You need to know what the correct output should be and compare it against what your code actually produces.

How Each Type Appears

Error TypeWhen DetectedError Message?How to Find
SyntaxBefore runningYesEditor highlights, interpreter reports
RuntimeDuring executionYesStack trace shows location
LogicNever (by computer)NoTesting and careful review

Knowing which type you're dealing with shapes your debugging approach. Syntax and runtime errors give you clues; logic errors require you to become the detective.

See More

You need to be signed in to leave a comment and join the discussion