Discard
An error cannot be ignored by accident. Every call to a function that throws must be marked with try in one of its forms, so calling one as a bare statement, with nothing saying where its error goes, is a compile error — otherwise a failure could vanish without anyone noticing.
Sometimes ignoring it is exactly right, though. A cash machine whose receipt printer has run out of paper should still hand over the money. This lesson shows why the compiler is strict and how to tell it, in the code, that you mean it.
A bare call is refused
PrintReceipt throws and returns nothing, like the functions in Unit fallible: it either printed the receipt or it did not. Call it the way you call PrintLine and the compiler stops you:
PrintReceipt(printer, amount);
error: call to 'PrintReceipt' can throw 'OutOfPaper' but is not marked with 'try'
help: write 'try' before the expression
Binding the call to _ does not help: let _ = PrintReceipt(printer, amount); is refused the same way, because it does not say what happens to the error either. And plain try PrintReceipt(...) would pass the error on to the caller, which Withdraw cannot do — it does not declare throws.
Dropping an error on purpose
There are two deliberate spellings, and both say "I know this can fail, and it does not matter here":
try PrintReceipt(printer, amount) catch { _ => {} };
try? PrintReceipt(printer, 10);
The first catches every error with one _ arm and does nothing in it. catch is the subject of the next lesson; for now, read the line as one phrase. The second, try?, turns the error into an absent optional, and an optional that nobody looks at may be ignored. The third withdrawal in the output finds the paper gone, hands over the money anyway, and prints no receipt; the try? call at the end of Main prints nothing for the same reason.
Which spelling fits
The empty block {} in the catch arm stands in for the missing success, and an empty block has no value — it completes with (). So that phrase works only for a call that returns nothing. For a call that succeeds with a value, such as an int, the arm would have to produce an int, as in catch { _ => 0 }. try? drops the error of either kind.
| What you write | Result |
|---|---|
PrintReceipt(printer, amount); | error: the call is not marked with try |
let _ = PrintReceipt(printer, amount); | the same error: _ handles nothing |
try PrintReceipt(printer, amount); | passes the error on — only in a function that throws |
try PrintReceipt(printer, amount) catch { _ => {} }; | a deliberate drop, for a call that returns nothing |
try? PrintReceipt(printer, amount); | a deliberate drop, for any call |
Why PrintLine was never a problem
Only errors must be handled. PrintLine does not throw. It returns an optional, IoError?, saying whether the console write had trouble — and any value that is not an error may be ignored. That is why every lesson so far could call it as a bare statement.
The program
The whole lesson is one package in the Examples repository. Its comments explain every step.
// An error cannot be ignored by accident. Every call to a function that throws must be marked
// with `try` in one of its forms, so calling one as a bare statement, with nothing saying where
// its error goes, is a compile error — otherwise a failure could vanish without anyone noticing.
//
// Sometimes ignoring it is exactly right, though. A cash machine whose receipt printer has run
// out of paper should still hand over the money. For that the error is dropped on purpose, in
// one of two spellings:
//
// try PrintReceipt(printer, amount) catch { _ => {} };
// try? PrintReceipt(printer, amount);
//
// Both say, in the code, "I know this can fail and it does not matter here". `catch` is the
// subject of a later lesson; for now, read the first as that one phrase. `try?` turns the error
// into an absent optional, which a statement is then free to ignore.
import Io::PrintLine;
struct Printer {
paper: int;
}
struct OutOfPaper {
}
// Throws and returns nothing: it either printed the receipt or it did not.
func PrintReceipt(printer: &var Printer, amount: int) throws OutOfPaper {
if printer.paper == 0 {
throw OutOfPaper {};
}
printer.paper -= 1;
PrintLine(" receipt: {} taken", amount);
}
func Withdraw(printer: &var Printer, amount: int) {
PrintLine("hand over {}", amount);
// The line below does not compile:
//
// PrintReceipt(printer, amount);
//
// error: call to 'PrintReceipt' can throw 'OutOfPaper' but is not marked with 'try'
// help: write 'try' before the expression
//
// Writing `let _ = PrintReceipt(printer, amount);` is refused the same way: binding the call
// to `_` does not say what happens to its error either. And plain `try` would pass the error
// on, which `Withdraw` cannot do, because it does not declare `throws`.
// The deliberate drop: a missing receipt is not worth stopping for. The empty block `{}`
// stands in for the missing success, so this spelling works only for a call that returns
// nothing. For a call that succeeds with a value, such as an `int`, the arm would have to
// produce an `int`; `try? F();` drops the error of either kind.
try PrintReceipt(printer, amount) catch { _ => {} };
}
func Main() -> int {
var printer = Printer { paper: 2 };
Withdraw(printer, 20);
Withdraw(printer, 50);
Withdraw(printer, 10);
// The same drop with `try?`. The printer is out of paper by now, so this prints nothing, and
// the error is gone without a word.
try? PrintReceipt(printer, 10);
// A value is a different matter: only errors must be handled. `PrintLine` does not throw. It
// returns an optional, `IoError?`, saying whether the console write had trouble, and any
// value that is not an error may be ignored — which is why every lesson so far could call it
// as a bare statement.
return 0;
}
Run it
cd Examples/Errors/Discard
rux run
hand over 20
receipt: 20 taken
hand over 50
receipt: 50 taken
hand over 10
Common mistakes
Try it yourself
- Start the printer with no paper at all. What does the output look like?
- Replace the discard with a
catcharm that prints" no receipt: out of paper". - Write the bare call
PrintReceipt(printer, amount);back in and read the error and its help line.
Learn more
- Catch — the full form of
catch, with one arm per error - Catch fallback —
catch { _ => ... }with a real fallback value - Ignoring an error in the Rux Reference