12.5.11    error

Description:   Creates a user defined error

Syntax:         error message ?info? ?code?


Argument:     message, info, code

Returns:        The return value from the expression

See Also:       catch, 12.10.8 Trap and display errors (Debug a Script).



Examples:      #example1

if {[lsearch $swl $sw] == -1} {

                                error "invalid config option, must be one of $swl"


The error command creates a user defined error code.  It also returns a TCL_ERROR code that causes command interpretation to be unwound.


Message is a string that is returned to the application to indicate what went wrong.


If the info argument is present the command containing the error command will not appear in errorInfo, the global variable. errorInfo is used to accumulate a trace of what was in progress when an error occurred.  Normally, as commands execute, the Tcl interpreter adds information to errorInfo. Using this option, the error will be placed in info. This feature is most useful in conjunction with the catch command: if a caught error cannot be handled successfully, info 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


If the code argument is present, its value is stored in the errorCode global variable. This variable holds a machine-readable description of the error.

See the tclvars manual for information on the proper format for the variable. If the code argument is not present, then errorCode is automatically reset to ‘‘NONE’’ by the Tcl interpreter as part of processing the error generated by the command.

See also 12.10.8 Trap and display errors (Debug a Script).