Module 19 · Debugging, Testing and Best Practices — topic 2 of 15
Programming errors
The three kinds of programming errors — syntax errors, runtime errors and logic errors — how each shows up, how to find it, and examples of each in Python.
Learning objectives
- Classify errors as syntax, runtime or logic
- Know how each kind is detected
- Use tests to catch logic errors
Definition
Programming errors fall into three groups: syntax errors break the language's grammar and stop the program from running; runtime errors (exceptions) occur while the program runs; logic errors make the program run without crashing but produce wrong results.
Introduction
A missing colon, a division by zero and a wrongly calculated leap year are very different problems. Recognising the kind of error tells you where to look.
Why it matters
Syntax and runtime errors announce themselves; logic errors hide. Knowing the difference makes you test for the dangerous, quiet ones.
Detailed explanation
Syntax errors are found before the program runs: Python refuses to compile the file and points at the problem (expected ':', unclosed bracket, bad indentation).
Runtime errors are exceptions raised during execution: NameError, TypeError, IndexError, ZeroDivisionError and so on. They appear only when the faulty line runs with problematic data.
Logic errors produce wrong output without any message: a wrong formula, a reversed condition, an off-by-one loop.
Editors and linters catch most syntax errors as you type.
Runtime errors are handled with try/except where recovery is possible, or fixed when they indicate a bug.
Logic errors are found by comparing actual results with expected results — by hand checks, assertions and automated tests.
Edge cases (empty input, zero, boundaries such as 1900 for leap years) are where logic errors usually hide.
Warnings (such as SyntaxWarning for invalid escape sequences) are not errors but often point to real mistakes.
Syntax
- Syntax: won't run.
- Runtime: crashes while running.
- Logic: runs, but wrong.
Code examples
A syntax error caught by compile()
SyntaxError: expected ':'
Runtime errors
IndexError ValueError NameError
More examples
A logic error found by edge cases
2024 True True 1900 True False 2000 True True 2026 False False
Step-by-step explanation
- Both versions agree on ordinary years such as 2024 and 2026.
- Only the century year 1900 exposes the logic error: it is divisible by 4 but not a leap year.
- Without that edge case, the wrong version would look correct.
Expected output
Running “A logic error found by edge cases” prints exactly:
Practical applications
- Billing systems with wrong rounding — a logic error that costs money.
- Crash reports from runtime errors in production.
Best practices
- Let your editor flag syntax errors early.
- Read exception messages fully.
- Test edge cases to catch logic errors.
Common mistakes
Assuming no crash means correct
Avoid
Prefer
Logic errors don't crash; only checks reveal them.
Important notes
- Static type checkers find some logic errors (such as passing a string where a number is expected) without running the code.
Key takeaways
- Syntax errors stop the program from running.
- Runtime errors are exceptions during execution.
- Logic errors give wrong results silently.
Try it yourself
- 1Write one example of each error type and explain how you would detect it.
- 2Find the logic error in a function that should return the largest of three numbers but fails when two are equal.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedA program runs without crashing but reports the wrong total. What kind of error is this?