⌂ Home

Lesson 7 — When things go wrong

Step 1: the two tenses of trouble

Things go wrong in two tenses. Some problems can be proven before the program runs — those are the checker's, and you met it in Lesson 5. The rest only arise along the way: a file that is not there, a division by a number that turned out to be zero. Those belong to attempt, which you borrowed in Lesson 6. Here is its full story.

attempt runs a stretch, and if a problem arises inside it, the if it fails stretch runs instead, with the problem's own words waiting in the name problem:

Runs right here, in your browser.

The book says it answers:

That sum went wrong: I can't divide by zero. Dividing needs a second number that isn't zero.
The program carries on.

Notice the zero here was worked out while the program ran — length("") is 0, but nothing in the program says so in so many words, so the checker cannot prove it ahead of time. The problem arose honestly, along the way, and attempt caught it, and the program carried on.

Your turn. Try dividing 7 by a worked-out zero of your own — length("ab") - 2 is one — and in the failure stretch, say "Caught it." Then say "Still standing." after the whole attempt.

The worked answer — try yours first, then peek
Runs right here, in your browser.

The book says it answers:

Caught it.
Still standing.

Step 2: the other tense

A zero written plainly — 10 / 0 — is the other tense: the checker proves it before the program starts and refuses the whole program, and attempt never gets the chance. The two of them work together: the checker catches what can be proven ahead of time, and attempt catches the rest, as it happens. Neither is a punishment. Both are the language refusing to let a program lie about what it is doing.

Step 3: the program's second voice

A program also has two voices. say speaks to the ordinary output; its sterner sibling sayerr speaks to the error stream, where warnings belong, so the ordinary output stays clean:

Runs right here, in your browser.

The book says it answers:

All is well.

The first line was spoken too — it simply went to the other stream, which this book's examples do not show. When you run that program yourself, your terminal will show both lines; they arrived by different roads.

Your turn. Send the warning "Low bridge." to the error stream, and say "Through." to the ordinary one.

The worked answer — try yours first, then peek
Runs right here, in your browser.

The book says it answers:

Through.

Step 4: reading a refusal

Most errors you will ever meet are neither deep nor dramatic. They are small, and the language's plain words make them easy to find. This program is shown, not run — read its refusal:

let mesage = "Shalom"
say message
Line 2: I don't know a variable called 'message' yet.
You can make one with: let message = ...
Did you mean 'mesage'?

One letter out of place, and the language not only found it but guessed what you meant. That is the spirit of every refusal in this language: name the line, say the problem plainly, point at the fix. When you meet one — and you will, everyone does — read it slowly. It is almost always telling you the truth.

The ledger grows: a book that survives a missing file

The ledger puts the two tenses to work. Reading its papers is wrapped in attempt, so the very first run — no file yet — is a beginning, not a crash:

Runs right here, in your browser.

The book says it answers:

A brand-new ledger.
Lines in the book: 0

The book now survives its own absence. In Lesson 11 this little pattern grows into the full loading of the ledger — gifts, numbers and all.

Your turn

The program below is broken in two places: one name is misspelled, and one sum can never be run. It is shown, not run:

let mesage = "Shalom"
say message
say 10 / 0

Fix both faults and make the program run clean.

The worked answer — try yours first, then peek
Runs right here, in your browser.

The book says it answers:

Shalom
5

You can now…

attempt … if it fails, and hear its own words in problem;

the checker, or caught as it happens by attempt;

ordinary output clean;

problem, the fix.

---

← Lesson 6: Papers that stayLesson 8: Standing on its own →