| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH error n "" Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
error \- Generate an error |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBerror \fImessage\fR ?\fIinfo\fR? ?\fIcode\fR? |
| 16 |
.BE |
| 17 |
|
| 18 |
.SH DESCRIPTION |
| 19 |
.PP |
| 20 |
Returns a \fBTCL_ERROR\fR code, which causes command interpretation to be |
| 21 |
unwound. \fIMessage\fR is a string that is returned to the application |
| 22 |
to indicate what went wrong. |
| 23 |
.PP |
| 24 |
The \fB\-errorinfo\fR return option of an interpreter is used |
| 25 |
to accumulate a stack trace of what was in progress when an |
| 26 |
error occurred; as nested commands unwind, |
| 27 |
the Tcl interpreter adds information to the \fB\-errorinfo\fR |
| 28 |
return option. If the \fIinfo\fR argument is present, it is |
| 29 |
used to initialize the \fB\-errorinfo\fR return options and |
| 30 |
the first increment of unwind information |
| 31 |
will not be added by the Tcl interpreter. |
| 32 |
In other |
| 33 |
words, the command containing the \fBerror\fR command will not appear |
| 34 |
in the stack trace; in its place will be \fIinfo\fR. |
| 35 |
Historically, this feature had been most useful in conjunction |
| 36 |
with the \fBcatch\fR command: |
| 37 |
if a caught error cannot be handled successfully, \fIinfo\fR can be used |
| 38 |
to return a stack trace reflecting the original point of occurrence |
| 39 |
of the error: |
| 40 |
.PP |
| 41 |
.CS |
| 42 |
catch {...} errMsg |
| 43 |
set savedInfo $::errorInfo |
| 44 |
\&... |
| 45 |
\fBerror\fR $errMsg $savedInfo |
| 46 |
.CE |
| 47 |
.PP |
| 48 |
When working with Tcl 8.5 or later, the following code |
| 49 |
should be used instead: |
| 50 |
.PP |
| 51 |
.CS |
| 52 |
catch {...} errMsg options |
| 53 |
\&... |
| 54 |
return -options $options $errMsg |
| 55 |
.CE |
| 56 |
.PP |
| 57 |
If the \fIcode\fR argument is present, then its value is stored |
| 58 |
in the \fB\-errorcode\fR return option. The \fB\-errorcode\fR |
| 59 |
return option is intended to hold a machine-readable description |
| 60 |
of the error in cases where such information is available; see |
| 61 |
the \fBreturn\fR manual page for information on the proper format |
| 62 |
for this option's value. |
| 63 |
.SH EXAMPLE |
| 64 |
.PP |
| 65 |
Generate an error if a basic mathematical operation fails: |
| 66 |
.PP |
| 67 |
.CS |
| 68 |
if {1+2 != 3} { |
| 69 |
\fBerror\fR "something is very wrong with addition" |
| 70 |
} |
| 71 |
.CE |
| 72 |
.SH "SEE ALSO" |
| 73 |
catch(n), return(n) |
| 74 |
.SH KEYWORDS |
| 75 |
error, exception |
| 76 |
'\" Local Variables: |
| 77 |
'\" mode: nroff |
| 78 |
'\" End: |