Identifying & Correcting
Errors
Learn to find, classify, and fix bugs in programs: from syntax and logic errors to runtime exceptions and beyond.
Why Errors Matter
Every program, no matter how carefully written, can contain errors (also called bugs). Identifying and correcting errors is one of the most important skills a programmer develops. In the real world, a single undetected bug can crash an app, corrupt data, or produce wrong results.
The Three Types of Errors
The Error Lifecycle
Debugging is an iterative process: you write code, test it, find errors, fix them, then test again. Professional developers spend roughly 50% of their time debugging.
Syntax Errors Explained
A syntax error occurs when your code violates the grammar rules of the programming language. The interpreter or compiler catches these before execution and refuses to run the program.
Runtime Errors Explained
A runtime error occurs while the program is running. The syntax is valid, but an illegal operation is attempted during execution.
Logic Errors Explained
A logic error produces wrong output without crashing. The program runs to completion but doesn't do what you intended. These are the hardest to detect because there is no error message.
Key Distinction: Error Types
| Syntax | Runtime | Logic | |
|---|---|---|---|
| When caught | Before running | During execution | After testing output |
| Program runs? | No | Partially | Yes |
| Error message? | Yes | Yes | No |
| Difficulty to fix | Easy | Medium | Hard |
| Example | Missing paren | Divide by \(0\) | Wrong formula |
How to Debug: Step by Step
Hand-Tracing a Program
Hand-tracing means executing each line mentally (or on paper) and recording how variable values change. This is the single most powerful technique for finding logic errors.
| Iteration | x before | x after |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 4 |
| 3 | 4 | 8 |
Example 1: Finding a Syntax Error
The following program is supposed to check if a number is positive and display a message. Find and fix the error.
Example 2: Fixing a Runtime Error
This program calculates the average of scores in a list, but crashes for some inputs.
Example 3: Tracing a Logic Error
This procedure should find the maximum value in a list, but it returns the wrong answer. Can you spot the bug?
The Infamous Off-by-One Error
An off-by-one error is a special type of logic error where a loop iterates one too many or one too few times, or an index is shifted by one position.
Common Mistakes to Avoid
Debugging Strategy Summary
| Strategy | Best For | How It Works |
|---|---|---|
| Read error messages | Syntax, Runtime | Locate line and error type |
| Add DISPLAY | Logic, Runtime | Print values to trace flow |
| Hand-tracing | Logic | Step through on paper |
| Test edge cases | All types | Try unusual inputs |
| Rubber duck | Logic | Explain code aloud line by line |
Question 1 of 5
A student writes a program that runs but always outputs \(0\) instead of the correct sum. What type of error is this?
Question 2 of 5
Consider the following code:
\( \text{nums} \leftarrow [10, 20, 30] \)
\( \text{result} \leftarrow \text{nums}[4] \)
\( \text{DISPLAY}(\text{result}) \)
What happens when this code executes?
Question 3 of 5
A procedure is intended to count how many times the value target appears in a list.
\( \text{PROCEDURE countOccurrences(lst, target)} \)
\( \{ \)
\( \text{count} \leftarrow 0 \)
\( \text{FOR EACH item IN lst} \)
\( \{ \)
\( \text{IF (item = target)} \)
\( \{ \)
\( \text{count} \leftarrow \text{count} + 1 \)
\( \} \)
\( \} \)
\( \text{RETURN count} \)
\( \} \)
A student calls \(\text{countOccurrences}([3, 7, 3, 3], 3)\) and gets \(3\). Is this correct, and what type of error (if any) exists?
Question 4 of 5
Consider the following code intended to double every element in a list:
\( i \leftarrow 1 \)
\( \text{REPEAT UNTIL } (i > \text{LENGTH}(\text{lst})) \)
\( \{ \)
\( \text{lst}[i] \leftarrow \text{lst}[i] * 2 \)
\( i \leftarrow i + 2 \)
\( \} \)
What error does this code contain?
Question 5 of 5
Which debugging strategy is most effective for finding a logic error when the program produces no error message?