Locate and identify the different types of errors

12.3 Program Testing and Maintenance

Objective

Locate and identify the different types of errors that can occur in a program. 🧩

Types of Errors

Error Type What It Looks Like Analogy Example
Syntax Error Compiler or interpreter stops with a message like “unexpected token”. Like a sentence that breaks the rules of grammar. `if (x > 5 {`  ← missing parenthesis.
Runtime Error Program crashes or throws an exception while running. Like a car that stalls when you hit the accelerator. `int a = 5 / 0;`  ← division by zero.
Logical Error Program runs but gives wrong results. Like following a recipe but swapping salt for sugar. `if (score >= 60) { pass = true; }`  ← should be `>= 50`.

How to Spot Each Error

  1. Syntax Errors: Read the compiler’s error message carefully. It usually points to the line and the exact token that caused the problem. 🛠️
  2. Runtime Errors: Use a debugger or add print statements to see where the program stops. Look for operations that can fail (division, file I/O, null references). ⚠️
  3. Logical Errors: Verify the program’s output against expected results. Write test cases for edge conditions (e.g., 0, negative numbers, maximum input). 🔍

Exam Tips

  • Read the question carefully: it often hints at the error type (e.g., “the program crashes” → runtime).
  • Show the error message or the line number if you can. It demonstrates you understand the compiler’s feedback.
  • Explain why the error occurs and how you would fix it. Use the analogy to make your answer memorable.
  • When writing code snippets, keep them short and to the point. Use comments to explain the bug.

Quick Reference Cheat‑Sheet

Error Typical Message Fix
Syntax “Unexpected identifier” Check brackets, commas, and spelling.
Runtime “Division by zero” Add a guard: `if (denominator != 0)`.
Logical Program runs but output is wrong. Re‑evaluate the algorithm or conditions.

Revision

Log in to practice.

1 views 0 suggestions