On this concept: Formula

Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Error Types

Error types are the main categories of program mistakes: syntax errors (the code is written incorrectly and won't run), runtime errors (the program crashes while running), and logic errors (the program runs but gives the wrong answer).

errors={syntax,runtime,logic}

Quick Answer

Error types are the main categories of program mistakes: syntax errors (the code is written incorrectly and won't run), runtime errors (the program crashes while running), and logic errors (the program runs but gives the wrong answer). Naming the type tells you where to look. A syntax error comes with a message pointing to a line. A runtime error appears only for some inputs or situations, such as dividing by zero or reading past the end of a list. A logic error gives no message at all, so you find it by comparing the output with an answer you worked out yourself.

Why This Matters

Each type needs a different search: syntax errors are found by reading the error message, runtime errors by finding the input that triggers the crash, and logic errors only by testing against known answers. Logic errors are the dangerous ones. A grade calculator that averages wrongly still prints a neat, believable number, and nobody notices until someone checks by hand.

Intuitive Explanation

Think of a program as instructions for a very literal helper. A syntax error is a sentence the helper cannot read at all, such as print('Hello with the closing quote and bracket missing. In Python the program will not start; the message names the line. A runtime error is an instruction the helper can read but cannot carry out at that moment: total / count when count is 0, or list[10] in a list of 5 items. The program starts, runs fine up to that line, then stops with a message.

A logic error is an instruction the helper can read and carry out, but it is the wrong instruction. average = a + b / 2 runs without complaint, but because division happens before addition it computes a + (b / 2): for 80 and 90 it gives 125 instead of 85. No message appears. The only clue is an output that does not match the answer you expected.

The three types show up at different times: syntax errors before the program runs, runtime errors while it runs, and logic errors only afterwards, when someone checks the result. A crash can still have a logic cause. If a loop runs one step too many and reads list[5] in a 5-item list, the crash is a runtime error, but the mistake is a logic error in the loop. Naming the type is the first step of debugging, not the whole job.

Think of it like this. Error types are like mistakes in a recipe. A smudged line nobody can read is a syntax error: the cook cannot use the recipe. 'Add the eggs' when there are no eggs is a runtime error: cooking stops partway. 'Bake at 400 °C' instead of 200 °C is a logic error: every step is done exactly as written, and the cake is ruined.

Core idea. Different bugs need different debugging strategies.


When to Use

Use error types when you have to classify a problem in a program from an error message, a crash, or an output that is simply wrong. Clues: 'SyntaxError' or 'won't start' point to syntax; 'division by zero' or 'index out of range' partway through point to runtime; 'runs but the total is wrong' points to logic. Once the type is named, finding and fixing the actual line is Debugging. Choosing the inputs that reveal errors is Testing.

Tip. Ask: Is the job to label a mistake as one that stops the code starting, crashes it partway, or gives a wrong answer?

How to Recognize It

Use what the program actually did, refuse to start, crash, or finish with a result, to decide which kind of mistake you have.

  1. Did the program refuse to start, with a message pointing at a line?

    Yes is a syntax error, such as a missing bracket. If it started fine, look at whether it crashed or finished, which points to a runtime or a logic error.

  2. Did it start, then stop partway with an error message, only for some inputs?

    Yes is a runtime error, like dividing by zero. If it never stopped and simply printed a wrong result, it is a logic error.

  3. Are you only asked to name the kind of mistake, not to find and fix the line?

    Yes is about error types. If you must track down the cause and correct the code, the task is Debugging.

  4. Has the mistake already shown up, rather than being something you are trying to catch?

    Yes means you are classifying a known error. If you are choosing inputs to find out whether any mistake exists, that is Testing.

Error Types vs Debugging vs Testing vs Edge Cases

All four come up when code goes wrong, so they get swapped. Ask whether you are naming the kind of mistake, hunting down its cause, checking whether mistakes exist, or picking the tricky inputs to try.

Error Types

Meaning
Error types sort program mistakes into syntax errors, runtime errors and logic errors by what the program does.
Key test
The answer is a category name: syntax, runtime or logic.
Formula
errors={syntax,runtime,logic}
Example
print('hi' with no closing bracket is a syntax error, because the program cannot even start.

Debugging

Meaning
Debugging is the work of finding the line that causes a known problem and correcting it.
Key test
You must locate and fix the mistake, not just name it.
Example
Tracing why a score always shows 0 and finding that the variable is reset inside the loop.

Testing

Meaning
Testing runs a program on chosen inputs and compares the results with answers decided in advance.
Key test
You are checking whether any mistake exists yet.
Formula
oexpected=oactual
Example
Trying a tip calculator with a $20 bill at 10% and expecting $2.

Edge Cases

Meaning
Edge cases are inputs at the limits of what a program should accept, where mistakes often hide.
Key test
The question is which extreme inputs to try, like zero or empty.
Example
Checking a countdown timer started at 0 seconds, and one started at the largest allowed time.

Formula & Notation

errors={syntax,runtime,logic}
Programming errors are commonly classified as syntax violations caught by the parser, runtime failures triggered during execution, and logic defects that preserve execution but violate correctness.

Worked Examples

Example 1 — Classify three bugs in one program

Easy

A program prints the average of a class's scores. Three versions each have one bug. (A) The first line is print('Average:' with no closing bracket. (B) It divides the total by the number of scores and crashes when the list of scores is empty. (C) It always divides the total by 10, but the class has 25 students. Name each error type and say how you would find it.
Show the steps and the answer
  1. Version A: the bracket is missing, so the code cannot be read and nothing runs. Syntax error.

    The interpreter rejects the file before running any line, and its message points to the line with the missing bracket.

  2. Version B: the code is valid and works for most classes, but with 0 scores it computes total / 0 and stops. Runtime error.

    It appears only for certain inputs, so you find it by running the case that crashes, here an empty list.

  3. Version C: it runs and prints a number with no complaint, but the number is wrong. Logic error.

    Nothing stops the program, so only comparing with a hand-worked answer reveals the problem.

  4. Check C with numbers: scores totaling 2,000 for 25 students should average 2,000 ÷ 25 = 80, but the program prints 2,000 ÷ 10 = 200.

    A known answer turns 'looks fine' into a clear mismatch.

Answer: A: syntax error (read the error message). B: runtime error (run the empty-list case). C: logic error (compare with a worked answer: it prints 200 instead of 80).

Takeaway: When the error appears tells you its type: before the run is syntax, during the run is runtime, and a wrong final answer is logic.

Example 2 — A runtime crash with a logic cause

Standard

names = ['Ana', 'Ben', 'Cy', 'Dee', 'Eli']. A loop runs for i from 0 to 5 and prints names[i]. It prints the five names, then crashes with 'index out of range'. What type of error does the message show, what mistake caused it, and what is the fix?
Show the steps and the answer
  1. Read the message: the crash happens while the program runs, at names[5]. That is a runtime error.

    The code is valid and printed five names successfully, so it is not a syntax error.

  2. Ask why names[5] was read: the loop goes up to 5, but 5 names use indexes 0 to 4.

    The program did exactly what the loop said; the loop's limit is the wrong instruction, a logic error.

  3. Fix the cause: loop while i < len(names), that is, from 0 to 4.

    Correcting the limit removes the crash and keeps the loop right for any number of names.

  4. Compare with catching the error and ignoring it: the message disappears, but the loop is still wrong.

    Hiding a runtime error does not fix the logic error that produced it.

Answer: The message shows a runtime error ('index out of range' at names[5]), caused by a logic error in the loop limit. Looping while i < len(names) fixes both.

Takeaway: Classify by what you see, then trace back: a runtime crash often starts with a logic mistake a few lines earlier.

Example 3 — Find the mistake: not every bug is syntax

Application

A program converts °C to °F with f = c × 9 / 5 + 23. For c = 100 it prints 203. Tomás writes: "It must be a syntax error, so I'll look for a missing bracket." Find the mistake and correct it.
Show the steps and the answer
  1. Check whether the program ran: it printed 203, so the code was read and carried out.

    A syntax error stops a program before it prints anything, so this cannot be one.

  2. Check for a crash: there was none, the program finished normally.

    No crash rules out a runtime error as well.

  3. Compare with a known answer: water boils at 100 °C, which is 212 °F, but the program printed 203.

    Running to the end with a wrong result is the sign of a logic error.

  4. Find the wrong instruction: 100 × 9 / 5 = 180, and 180 + 23 = 203. The formula should add 32, not 23.

    Working the formula by hand with the same input shows which step differs.

  5. Fix and re-check: f = c × 9 / 5 + 32 gives 180 + 32 = 212 for 100 °C and 0 + 32 = 32 for 0 °C.

    Two known values, boiling and freezing, confirm the corrected formula.

Answer: It is a logic error, not a syntax error. Changing + 23 to + 32 gives 212 °F for 100 °C.

Takeaway: A program that prints a wrong answer has a logic error; searching for missing brackets will not find it.

Where You'll See It

  • A missing parenthesis stops the code

    Leaving out a closing bracket is a syntax error — the program will not even start, like a sentence with mismatched quotation marks that no one can read.

  • Dividing by a zero the user typed

    The code is written fine and starts running, but when it tries 10 / 0 it crashes mid-way — that is a runtime error, a problem that only appears while the program is actually running.

  • A calculator that adds when it should subtract

    The program runs to the end and prints an answer, but the answer is wrong because you typed + instead of - — that is a logic error, the sneakiest kind because nothing crashes.

Common Mistakes

  1. The slip: If the program crashes, it must be a syntax error.

    Why it's tempting: Both syntax and runtime problems feel like 'broken code,' so students collapse them into one.

    Instead: Syntax errors stop the code from running at all (caught before/at compile time). A crash that happens partway through a running program is a runtime error, like dividing by zero or reading past the end of a list.

  2. The slip: If the program runs and prints something, there are no errors.

    Why it's tempting: A clean run with output looks like success.

    Instead: A logic error produces a running program that gives the WRONG answer. Running without crashing does not mean correct — you must check the output against expected results.

  3. The slip: Once the crash is gone, the bug is fixed.

    Why it's tempting: The error message was the only visible sign of trouble, so when it disappears the program looks healthy.

    Instead: A crash can have a logic cause: a loop that runs one step too many reads list[5] in a 5-item list. Adding a check that skips index 5 stops the crash but leaves the loop wrong, so fix the loop's range and compare the output with an answer you worked out by hand.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. A program should print the average of [4, 8, 12], but it divides the total by len(numbers) + 1. It runs without crashing. What does it print, and what type of error is this?

    Hint: Work out the total and the wrong divisor. Did the program stop early?

  2. numbers = [3, 6, 9], then print(numbers[3]). The program starts, then stops with an IndexError. Which error type is this?

    Hint: The code is written correctly. When does the problem show up?

  3. Classify each: (a) x = 10 / 0 crashes when it runs; (b) area = length + width for a rectangle runs and prints a wrong area; (c) print('hi' is missing its closing bracket.

    Hint: Does it run at all? Does it crash while running? Does it run but give a wrong answer?

  4. Lines 1 to 6 of a program print their output, then it stops with 'NameError: name totl is not defined' on line 7. Syntax, runtime or logic error?

    Hint: Did any lines run before the message appeared?

  5. Task A: 'Say what kind of error makes this program crash.' Task B: 'Find the line that makes the total wrong and change it so the total is right.' Which task is Error Types and which is Debugging?

    Hint: One task labels the problem. The other repairs it.

  6. A converter runs fully but prints about 37.6 for 10 °C, because the code uses F = C × 5 / 9 + 32. A student says: 'It's a syntax error.' Correct them.

    Hint: Did the program run? Check the conversion formula.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Is an infinite loop a runtime error or a logic error?

Usually a logic error. The program is written correctly and nothing crashes; it just keeps going because its stopping condition can never become true, such as counting up while waiting for the number to reach 0. Some systems eventually stop it and report a timeout, which looks like a runtime error, but the cause is still a mistake in the program's logic.

Does the error message always point to the right line?

Not always. With a missing closing bracket, many languages report the error on the next line, or even at the end of the file, because that is where the computer finally notices something is wrong. So when a message names a line that looks perfect, check the line just above it for a bracket or quotation mark that was never closed.

Is a typo in a variable name a syntax error?

Usually not, which surprises people. In Python, writing totl instead of total is perfectly good syntax, so the program starts. It fails only when that line runs and Python finds no variable named totl, which is a runtime error called a NameError. In a language like Java, the compiler checks names before running, so the same typo is caught before the program starts.

Learning Path

← Before

Debugging
Error Types

You are here

Next →

Testing
You need Debugging first: the process of finding and fixing a fault. Error Types adds three labels, syntax, runtime and logic, each with its own way of being found. Next, Testing shows how to choose inputs with known answers, the main way to catch logic errors, since they produce no error message.

See Also