Errors
An error in Tcl stops the script and goes up through each caller, until a catch or a try stops it. An error has a message and an error code: a list that code can match. This lesson makes add check its input.
Give an error
The box gives add two checks. Each error has a code that starts with JOT, so that a caller can tell the errors of jot from other errors.
proc add {text} {
global notes
if {[string trim $text] eq ""} {
return -code error -errorcode {JOT EMPTY} "a note needs text"
}
if {[string length $text] > 80} {
throw {JOT LONG} "a note has 80 characters at most"
}
lappend notes [dict create text $text done 0]
return [llength $notes]
}
add "buy milk" works. add "" stops with the message. error MESSAGE gives an error with the code NONE. throw CODE MESSAGE and return -code error -errorcode CODE set a code.
Stop an error with try
try runs a body and matches the result. trap matches the start of the error code, on error matches each error, and on ok matches a result. finally runs at the end in each case.
try {add ""} trap {JOT EMPTY} {msg} {set r "empty: $msg"}matches one error.try {add [string repeat x 90]} trap {JOT} {msg opts} {dict get $opts -errorcode}matches each error ofjot, and reads the code from the options.try {expr {1/0}} on error {msg opts} {dict get $opts -errorcode}shows the code of an error of Tcl.
catch is the older form. catch {add ""} msg gives 1 for an error and puts the message in msg. The variable errorInfo has the message and the stack of the last error. Run set errorInfo to see the stack, and lindex [split $errorInfo \n] 0 to get its first line.
A catch that hides each error
catch {...} with no check of its result hides each error: a typo, a missing variable, and a full disk look the same. proc save {} { catch {add ""}; return ok } and save give ok for a failure.
Reading code an agent wrote
- Each
catchchecks its result, or the code has a comment that says why the error does not matter. trywithtrapmatches one error code.on errormatches each error, and is right only at the top of a program or around a whole job.- An error that a caller can handle has an error code, and the message says what to change.
return -code errorin a proc points the error at the caller, anderrorpoints it at the line in the proc.
Ask an agent
Make
addgive an error with the codeJOT EMPTYfor empty text. In the caller, usetrywithtrap {JOT EMPTY}, and let each other error go up.
Try this
- Add a check to
addfor a note that is already innotes, with the codeJOT DUP. - Run
try {add ""} on ok {r} {set r} on error {m} {set m}. - Read
set errorCodeafter an error ofadd.
The state of the shell at the start of this lesson
set notes {}