Unit 2 · Programming 1 · Lesson 2.8

When Programs Go Wrong

Every program you’ll ever write will break. The difference between a beginner and a programmer is knowing which kind of broken you’re looking at.

Big Idea 1 — Creative DevelopmentFree exam points

Before this lesson — read on Runestone

The book’s error taxonomy. It maps onto the three types below. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Still in Programs. In 2.1 you met errors and were told not to panic. Today you get the vocabulary that makes that promise real — three named categories, and a different fix for each.

2

The Hook

Three ways to fail. The same little program can break before it starts, break halfway through, or run perfectly and still be wrong. Those are three completely different problems, and confusing them is why debugging feels like flailing.

Name the category and the fix becomes obvious. The exam knows this — “what type of error is this?” is one of the most reliable easy questions on the whole test. Today is about collecting those free points and never flailing again.

3

The Idea

Three kinds of error, told apart by when they bite:

TypeWhenTell
Syntaxbefore it runsBroke a grammar rule — missing quote, paren, or colon. Won’t start at all.
Run-timewhile runningLegal code hits something impossible — divide by zero, convert “hello” to int. Crashes partway.
Logicnever crashesRuns fine, gives the wrong answer. No error message — the sneakiest kind.

When a run-time error hits, Python prints a traceback — a few lines of red that look scary and are actually a map. Read it bottom-up: the last line names the error type and message; the line above points at where. That’s usually all you need.

Traceback (most recent call last):
  File "greet.py", line 3, in <module>
    age = int("twelve")
ValueError: invalid literal for int() with base 10: 'twelve'

Bottom line: a ValueError. The line above: it happened converting "twelve". Now you know it’s a run-time error and exactly where. That’s the whole skill.

Rubber-duck debugging. The oldest trick that works: explain your code out loud, line by line, to a rubber duck (or any patient object). Saying “this line should…” forces you to notice the line that doesn’t. Debugging is finding and fixing errors; testing is running the program on purpose to reveal them before a user does.
4

Try It — Name That Error

Four broken programs. For each, decide: does it break before running (syntax), during (run-time), or not at all but give a wrong answer (logic)? Pick, then see the verdict.

0 of 4 classified
5

Vocabulary

syntax error Programs
A violation of the language’s grammar rules. The program will not run at all until it’s fixed.
run-time error Programs
An error that occurs while the program is running — the code is legal, but it attempts something impossible, and it crashes at that point.
logic error Programs
An error where the program runs without crashing but produces the wrong result. There is no error message — only a wrong answer.
debugging · testing Programs
Debugging is finding and fixing errors. Testing is deliberately running a program to uncover them.
6

Check

Five questions in AP format. Pick an answer for instant feedback.

0 of 5 answered
7

Impact Check

The error that doesn’t announce itself

Syntax and run-time errors are loud — they stop the program and point at themselves. Logic errors are silent, and that makes them the dangerous ones. A program that computes the wrong dosage, the wrong tax, or the wrong grade runs perfectly. Nothing turns red. The only way to catch it is to test with inputs where you already know the right answer, and check that the program agrees.

This is why professionals write tests before trusting code, and why “it ran without errors” is not the same as “it’s correct.” You’ll build this instinct at the end of the unit (2.16, edge cases) — but it starts here, with the realization that the scariest bug is the one that never crashes.

8

Connections

Came from 2.1 — “errors are normal, read the message” now has a framework: three types, three fixes.
Uses 2.4 — the classic int("hello") crash is a run-time error, and now you can name it.
Next in 2.9 — if every program breaks, how does real software get built at all? The answer is a process, not a single perfect draft.
Returns on the exam — “identify the error type” is a recurring easy MCQ. Syntax = won’t start. Run-time = crashes midway. Logic = wrong answer, no crash.
← 2.7 Strings II