Skip to content
Tcl Source Code Artifact [2cbad1d600]
Login

Artifact [2cbad1d600]

Artifact 2cbad1d600a15ff6a97ce8460c13c615f8c9e47fe9fce5dc6bbdc68277225e7d:



CommandName: error ManualSection: n Version: unknown TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

error - Generate an error

Synopsis

::: {.synopsis} ::: [error]{.cmd} [message]{.arg} [info]{.optarg} [code]{.optarg} :::

Description

Returns a fBTCL_ERRORfR code, which causes command interpretation to be unwound. fIMessagefR is a string that is returned to the application to indicate what went wrong.

The fB-errorinfofR return option of an interpreter is used to accumulate a stack trace of what was in progress when an error occurred; as nested commands unwind, the Tcl interpreter adds information to the fB-errorinfofR return option. If the fIinfofR argument is present, it is used to initialize the fB-errorinfofR return options and the first increment of unwind information will not be added by the Tcl interpreter. In other words, the command containing the fBerrorfR command will not appear in the stack trace; in its place will be fIinfofR. Historically, this feature had been most useful in conjunction with the fBcatchfR command: if a caught error cannot be handled successfully, fIinfofR can be used to return a stack trace reflecting the original point of occurrence of the error:

catch {...} errMsg
set savedInfo $::errorInfo
\&...
error $errMsg $savedInfo

When working with Tcl 8.5 or later, the following code should be used instead:

catch {...} errMsg options
\&...
return -options $options $errMsg

If the fIcodefR argument is present, then its value is stored in the fB-errorcodefR return option. The fB-errorcodefR return option is intended to hold a machine-readable description of the error in cases where such information is available; see the fBreturnfR manual page for information on the proper format for this option's value.

Example

Generate an error if a basic mathematical operation fails:

if {1+2 != 3} {
    error "something is very wrong with addition"
}