error(3tcl)
π error β bestie, we're generating an error β¨
so this is error(3tcl), one of the Tcl Built-In Commands, and it's literally the drama queen of the interpreter β it shows up, it throws a fit, everything unwinds. iconic behaviour, ngl.
β¨ SYNOPSIS β¨
error message ?info? ?code?
π DESCRIPTION π
okay so when you call error, it returns a TCL_ERROR code, which makes command interpretation get unwound. like, the whole thing just starts unwinding. message is a string that gets handed back to the application so it knows what went wrong β that's literally its whole job, telling everyone what happened. π«Ά
Now the juicy part. The -errorinfo return option of an interpreter is what collects the stack trace of whatever was in progress when the error popped off. as nested commands unwind, the Tcl interpreter keeps adding info to that -errorinfo return option. it's giving receipts. π
if the info argument is there, then it gets used to initialize the -errorinfo return options, and the first increment of unwind information will not be added by the Tcl interpreter. in other words: the command that contains the error command won't show up in the stack trace β in its place you get info. cute little swap, right? π
historically, this feature was most useful when paired with the catch command: if a caught error can't be handled successfully, info can be used to return a stack trace that reflects the original point of occurrence of the error:
catch {...} errMsg
set savedInfo $::errorInfo
...
error $errMsg $savedInfo
but bestie, if you're working with Tcl 8.5 or later, use this instead:
catch {...} errMsg options
...
return -options $options $errMsg
and if the code argument is present, its value gets stored in the -errorcode return option. -errorcode is meant to hold a machine-readable description of the error, for when that kind of info is actually available. for the proper format of this option's value, go check the return manual page β iykyk. π₯
πΈ EXAMPLE πΈ
generate an error if a basic mathematical operation fails:
if {1+2 != 3} {
error "something is very wrong with addition"
}
(and honestly? if 1+2 != 3 then something is very wrong with addition. no cap. π)
π¦ SEE ALSO π¦
π KEYWORDS π
errorexception
ok that's the whole page, periodt. go forth and raise errors responsibly π
signing off from the Tcl manual, this was error(3tcl) β main character energy only. mwah. π
β¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms