Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/AddErrInfo.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 12.5 KB
Line 
1'\"
2'\" Copyright (c) 1989-1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1997 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'\" RCS: @(#) $Id: AddErrInfo.3,v 1.20 2007/12/13 15:22:30 dgp Exp $
9'\"
10.so man.macros
11.TH Tcl_AddErrorInfo 3 8.5 Tcl "Tcl Library Procedures"
12.BS
13.SH NAME
14Tcl_GetReturnOptions, Tcl_SetReturnOptions, Tcl_AddErrorInfo, Tcl_AppendObjToErrorInfo, Tcl_AddObjErrorInfo, Tcl_SetObjErrorCode, Tcl_SetErrorCode, Tcl_SetErrorCodeVA, Tcl_PosixError, Tcl_LogCommandInfo \- retrieve or record information about errors and other return options
15.SH SYNOPSIS
16.nf
17\fB#include <tcl.h>\fR
18.VS 8.5
19.sp
20Tcl_Obj *
21\fBTcl_GetReturnOptions\fR(\fIinterp, code\fR)
22.sp
23int
24\fBTcl_SetReturnOptions\fR(\fIinterp, options\fR)
25.VE 8.5
26.sp
27\fBTcl_AddErrorInfo\fR(\fIinterp, message\fR)
28.VS 8.5
29.sp
30\fBTcl_AppendObjToErrorInfo\fR(\fIinterp, objPtr\fR)
31.VE 8.5
32.sp
33\fBTcl_AddObjErrorInfo\fR(\fIinterp, message, length\fR)
34.sp
35\fBTcl_SetObjErrorCode\fR(\fIinterp, errorObjPtr\fR)
36.sp
37\fBTcl_SetErrorCode\fR(\fIinterp, element, element, ... \fB(char *) NULL\fR)
38.sp
39\fBTcl_SetErrorCodeVA\fR(\fIinterp, argList\fR)
40.sp
41const char *
42\fBTcl_PosixError\fR(\fIinterp\fR)
43.sp
44void
45\fBTcl_LogCommandInfo\fR(\fIinterp, script, command, commandLength\fR)
46.SH ARGUMENTS
47.AS Tcl_Interp commandLength
48.AP Tcl_Interp *interp in
49Interpreter in which to record information.
50.AP int code
51The code returned from script evaluation.
52.AP Tcl_Obj *options
53A dictionary of return options.
54.AP char *message in
55For \fBTcl_AddErrorInfo\fR,
56this is a conventional C string to append to the \fB\-errorinfo\fR return option.
57For \fBTcl_AddObjErrorInfo\fR,
58this points to the first byte of an array of \fIlength\fR bytes
59containing a string to append to the \fB\-errorinfo\fR return option.
60This byte array may contain embedded null bytes
61unless \fIlength\fR is negative.
62.VS 8.5
63.AP Tcl_Obj *objPtr in
64A message to be appended to the \fB\-errorinfo\fR return option
65in the form of a Tcl_Obj value.
66.VE 8.5
67.AP int length in
68The number of bytes to copy from \fImessage\fR when
69appending to the \fB\-errorinfo\fR return option.
70If negative, all bytes up to the first null byte are used.
71.AP Tcl_Obj *errorObjPtr in
72The \fB\-errorcode\fR return option will be set to this value.
73.AP char *element in
74String to record as one element of the \fB\-errorcode\fR return option.
75Last \fIelement\fR argument must be NULL.
76.AP va_list argList in
77An argument list which must have been initialized using
78\fBva_start\fR, and cleared using \fBva_end\fR.
79.AP "const char" *script in
80Pointer to first character in script containing command (must be <= command)
81.AP "const char" *command in
82Pointer to first character in command that generated the error
83.AP int commandLength in
84Number of bytes in command; -1 means use all bytes up to first null byte
85.BE
86
87.SH DESCRIPTION
88.PP
89.VS 8.5
90The \fBTcl_SetReturnOptions\fR and \fBTcl_GetReturnOptions\fR
91routines expose the same capabilities as the \fBreturn\fR and
92\fBcatch\fR commands, respectively, in the form of a C interface.
93.PP
94\fBTcl_GetReturnOptions\fR retrieves the dictionary of return options
95from an interpreter following a script evaluation.
96Routines such as \fBTcl_Eval\fR are called to evaluate a
97script in an interpreter.  These routines return an integer
98completion code.  These routines also leave in the interpreter
99both a result and a dictionary of return options generated
100by script evaluation.  Just as \fBTcl_GetObjResult\fR retrieves
101the result, \fBTcl_GetReturnOptions\fR retrieves the dictionary
102of return options.  The integer completion code should be
103passed as the \fIcode\fR argument to \fBTcl_GetReturnOptions\fR
104so that all required options will be present in the dictionary.
105Specifically, a \fIcode\fR value of \fBTCL_ERROR\fR will
106ensure that entries for the keys \fB\-errorinfo\fR,
107\fB\-errorcode\fR, and \fB\-errorline\fR will appear in the
108dictionary.  Also, the entries for the keys \fB\-code\fR
109and \fB\-level\fR will be adjusted if necessary to agree
110with the value of \fIcode\fR.  The \fB(Tcl_Obj *)\fR returned
111by \fBTcl_GetReturnOptions\fR points to an unshared
112\fBTcl_Obj\fR with reference count of zero.  The dictionary
113may be written to, either adding, removing, or overwriting
114any entries in it, with the need to check for a shared object.
115.PP
116A typical usage for \fBTcl_GetReturnOptions\fR is to
117retrieve the stack trace when script evaluation returns
118\fBTCL_ERROR\fR, like so:
119.CS
120int code = Tcl_Eval(interp, script);
121if (code == TCL_ERROR) {
122    Tcl_Obj *options = Tcl_GetReturnOptions(interp, code); 
123    Tcl_Obj *key = Tcl_NewStringObj("-errorinfo", -1);
124    Tcl_Obj *stackTrace;
125    Tcl_IncrRefCount(key);
126    Tcl_DictObjGet(NULL, options, key, &stackTrace);
127    Tcl_DecrRefCount(key);
128    /* Do something with stackTrace */
129}
130.CE
131.PP
132\fBTcl_SetReturnOptions\fR sets the return options
133of \fIinterp\fR to be \fIoptions\fR.  If \fIoptions\fR
134contains any invalid value for any key, TCL_ERROR will
135be returned, and the interp result will be set to an
136appropriate error message.  Otherwise, a completion code
137in agreement with the \fB\-code\fR and \fB\-level\fR
138keys in \fIoptions\fR will be returned.
139.PP
140As an example, Tcl's \fBreturn\fR command itself could
141be implemented in terms of \fBTcl_SetReturnOptions\fR
142like so:
143.CS
144if ((objc % 2) == 0) { /* explicit result argument */
145    objc--;
146    Tcl_SetObjResult(interp, objv[objc]);
147}
148return Tcl_SetReturnOptions(interp, Tcl_NewListObj(objc-1, objv+1));
149.CE
150(It is not really implemented that way.  Internal access
151privileges allow for a more efficient alternative that meshes
152better with the bytecode compiler.)
153.PP
154Note that a newly created \fBTcl_Obj\fR may be passed
155in as the \fIoptions\fR argument without the need to tend
156to any reference counting.  This is analogous to
157\fBTcl_SetObjResult\fR.
158.PP
159While \fBTcl_SetReturnOptions\fR provides a general interface
160to set any collection of return options, there are a handful
161of return options that are very frequently used.  Most
162notably the \fB\-errorinfo\fR and \fB\-errorcode\fR return
163options should be set properly when the command procedure
164of a command returns \fBTCL_ERROR\fR.  Tcl provides several
165simpler interfaces to more directly set these return options.
166.VE 8.5
167.PP
168The \fB\-errorinfo\fR option holds a stack trace of the
169operations that were in progress when an error occurred,
170and is intended to be human-readable.
171The \fB\-errorcode\fR option holds a list of items that
172are intended to be machine-readable.
173The first item in the \fB\-errorcode\fR value identifies the class of
174error that occurred
175(e.g. POSIX means an error occurred in a POSIX system call)
176and additional elements hold additional pieces
177of information that depend on the class.
178See the tclvars manual entry for details on the various
179formats for the \fB\-errorcode\fR option used by
180Tcl's built-in commands.
181.PP
182The \fB\-errorinfo\fR option value is gradually built up as an
183error unwinds through the nested operations.
184Each time an error code is returned to \fBTcl_Eval\fR, or
185any of the routines that performs script evaluation,
186the procedure \fBTcl_AddErrorInfo\fR is called to add
187additional text to the \fB\-errorinfo\fR value describing the
188command that was being executed when the error occurred.
189By the time the error has been passed all the way back
190to the application, it will contain a complete trace
191of the activity in progress when the error occurred.
192.PP
193It is sometimes useful to add additional information to
194the \fB\-errorinfo\fR value beyond what can be supplied automatically
195by the script evaluation routines.
196\fBTcl_AddErrorInfo\fR may be used for this purpose:
197its \fImessage\fR argument is an additional
198string to be appended to the \fB\-errorinfo\fR option.
199For example, when an error arises during the \fBsource\fR command,
200the procedure \fBTcl_AddErrorInfo\fR is called to
201record the name of the file being processed and the
202line number on which the error occurred.
203Likewise, when an error arises during evaluation of a
204Tcl procedures, the procedure name and line number
205within the procedure are recorded, and so on.
206The best time to call \fBTcl_AddErrorInfo\fR is just after
207a script evaluation routine has returned \fBTCL_ERROR\fR.
208The value of the \fB\-errorline\fR return option (retrieved
209via a call to \fBTcl_GetReturnOptions\fR) often makes up
210a useful part of the \fImessage\fR passed to \fBTcl_AddErrorInfo\fR.
211.PP
212.VS 8.5
213\fBTcl_AppendObjToErrorInfo\fR is an alternative interface to the
214same functionality as \fBTcl_AddErrorInfo\fR.  \fBTcl_AppendObjToErrorInfo\fR
215is called when the string value to be appended to the \fB\-errorinfo\fR option
216is available as a \fBTcl_Obj\fR instead of as a \fBchar\fR array.
217.VE 8.5
218.PP
219\fBTcl_AddObjErrorInfo\fR is nearly identical
220to \fBTcl_AddErrorInfo\fR, except that it has an additional \fIlength\fR
221argument.  This allows the \fImessage\fR string to contain
222embedded null bytes.  This is essentially never a good idea.
223If the \fImessage\fR needs to contain the null character \fBU+0000\fR,
224Tcl's usual internal encoding rules should be used to avoid
225the need for a null byte.  If the \fBTcl_AddObjErrorInfo\fR
226interface is used at all, it should be with a negative \fIlength\fR value.
227.PP
228The procedure \fBTcl_SetObjErrorCode\fR is used to set the
229\fB\-errorcode\fR return option to the list object \fIerrorObjPtr\fR
230built up by the caller.
231\fBTcl_SetObjErrorCode\fR is typically invoked just
232before returning an error. If an error is
233returned without calling \fBTcl_SetObjErrorCode\fR or
234\fBTcl_SetErrorCode\fR the Tcl interpreter automatically sets
235the \fB\-errorcode\fR return option to \fBNONE\fR.
236.PP
237The procedure \fBTcl_SetErrorCode\fR is also used to set the
238\fB\-errorcode\fR return option. However, it takes one or more strings to
239record instead of an object. Otherwise, it is similar to
240\fBTcl_SetObjErrorCode\fR in behavior.
241.PP
242\fBTcl_SetErrorCodeVA\fR is the same as \fBTcl_SetErrorCode\fR except that
243instead of taking a variable number of arguments it takes an argument list.
244.PP
245\fBTcl_PosixError\fR
246sets the \fB\-errorcode\fR variable after an error in a POSIX kernel call.
247It reads the value of the \fBerrno\fR C variable and calls
248\fBTcl_SetErrorCode\fR to set the \fB\-errorcode\fR return
249option in the \fBPOSIX\fR format.
250The caller must previously have called \fBTcl_SetErrno\fR to set
251\fBerrno\fR; this is necessary on some platforms (e.g. Windows) where Tcl
252is linked into an application as a shared library, or when the error
253occurs in a dynamically loaded extension. See the manual entry for
254\fBTcl_SetErrno\fR for more information.
255.PP
256\fBTcl_PosixError\fR returns a human-readable diagnostic message
257for the error
258(this is the same value that will appear as the third element
259in the \fB\-errorcode\fR value).
260It may be convenient to include this string as part of the
261error message returned to the application in
262the interpreter's result.
263.PP
264\fBTcl_LogCommandInfo\fR is invoked after an error occurs in an
265interpreter.  It adds information about the command that was being
266executed when the error occurred to the \fB\-errorinfo\fR value, and
267the line number stored internally in the interpreter is set. 
268.PP
269In older releases of Tcl, there was no \fBTcl_GetReturnOptions\fR
270routine.  In its place, the global Tcl variables \fBerrorInfo\fR
271and \fBerrorCode\fR were the only place to retrieve the error
272information.  Much existing code written for older Tcl releases
273still access this information via those global variables.
274.PP
275It is important to realize that while reading from those
276global variables remains a supported way to access these
277return option values, it is important not to assume that
278writing to those global variables will properly set the
279corresponding return options.  It has long been emphasized
280in this manual page that it is important to
281call the procedures described here rather than
282setting \fBerrorInfo\fR or \fBerrorCode\fR directly with
283\fBTcl_ObjSetVar2\fR.
284.PP
285If the procedure \fBTcl_ResetResult\fR is called,
286it clears all of the state of the interpreter associated with
287script evaluation, including the entire return options dictionary.
288In particular, the \fB\-errorinfo\fR and \fB\-errorcode\fR options
289are reset. 
290If an error had occurred, the \fBTcl_ResetResult\fR call will
291clear the error state to make it appear as if no error had
292occurred after all.
293The global variables \fBerrorInfo\fR and
294\fBerrorCode\fR are not modified by \fBTcl_ResetResult\fR
295so they continue to hold a record of information about the
296most recent error seen in an interpreter.
297
298.SH "SEE ALSO"
299Tcl_DecrRefCount, Tcl_IncrRefCount, Tcl_Interp, Tcl_ResetResult, Tcl_SetErrno
300
301.SH KEYWORDS
302error, object, object result, stack, trace, variable
Note: See TracBrowser for help on using the repository browser.