#include <tcl.h> Tcl_AddObjErrorInfo(interp, message, length) Tcl_AddErrorInfo(interp, message) Tcl_SetErrorCode(interp, element, element, ... (char *) NULL) char * Tcl_PosixError(interp)
The errorInfo variable is gradually built up as an error unwinds through the nested operations. Each time an error code is returned to Tcl_EvalObj (or Tcl_Eval, which calls Tcl_EvalObj) it calls the procedure Tcl_AddObjErrorInfo to add additional text to errorInfo describing the command that was being executed when the error occurred. By the time the error has been passed all the way back to the application, it will contain a complete trace of the activity in progress when the error occurred.
It is sometimes useful to add additional information to errorInfo beyond what can be supplied automatically by Tcl_EvalObj. Tcl_AddObjErrorInfo may be used for this purpose: its message and length arguments describe an additional string to be appended to errorInfo. For example, the source command calls Tcl_AddObjErrorInfo to record the name of the file being processed and the line number on which the error occurred; for Tcl procedures, the procedure name and line number within the procedure are recorded, and so on. The best time to call Tcl_AddObjErrorInfo is just after Tcl_EvalObj has returned TCL_ERROR. In calling Tcl_AddObjErrorInfo, you may find it useful to use the errorLine field of the interpreter (see the Tcl_Interp manual entry for details).
Tcl_AddErrorInfo resembles Tcl_AddObjErrorInfo but differs in initializing errorInfo from the interpreter's string result interp->result instead of from interp->objResult if the error is just starting to be logged. It also takes a conventional C string in message instead of Tcl_AddObjErrorInfo's counted string.
The procedure Tcl_SetErrorCode is used to set the errorCode variable. Its element arguments give one or more strings to record in errorCode: each element will become one item of a properly-formed Tcl list stored in errorCode. Tcl_SetErrorCode is typically invoked just before returning an error. If an error is returned without calling Tcl_SetErrorCode then the Tcl interpreter automatically sets errorCode to NONE.
Tcl_PosixError sets the errorCode variable after an error in a POSIX kernel call. It reads the value of the errno C variable and calls Tcl_SetErrorCode to set errorCode in the POSIX format. The caller must previously have called Tcl_SetErrno to set errno; this is necessary on some platforms (e.g. Windows) where Tcl is linked into an application as a shared library, or when the error occurs in a dynamically loaded extension. See the manual entry for Tcl_SetErrno for more information.
Tcl_PosixError returns a human-readable diagnostic message for the error (this is the same value that will appear as the third element in errorCode). It may be convenient to include this string as part of the error message returned to the application in interp->objResult or interp->result.
It is important to call the procedures described here rather than setting errorInfo or errorCode directly with Tcl_ObjSetVar2. The reason for this is that the Tcl interpreter keeps information about whether these procedures have been called. For example, the first time Tcl_AddObjErrorInfo is called for an error, it clears the existing value of errorInfo and adds the error message in interp->objResult to the variable before appending message; in subsequent calls, it just appends the new message. When Tcl_SetErrorCode is called, it sets a flag indicating that errorCode has been set; this allows the Tcl interpreter to set errorCode to NONE if it receives an error return when Tcl_SetErrorCode hasn't been called.
If the procedures Tcl_ResetObjResult or Tcl_ResetResult are called, they clear all of the state associated with errorInfo and errorCode (but it doesn't actually modify the variables). If an error had occurred, this will clear the error state to make it appear as if no error had occurred after all.
Copyright © 1989-1994 The Regents of the University of California.
Copyright © 1994-1996 Sun Microsystems, Inc.