Tcl_AddErrorInfo(3)
π Tcl_AddErrorInfo(3) β Tcl Library Procedures π
okay bestie, this is the Tcl Library Procedures page for a whole squad of return-option and error-info functions. The vibe? retrieve or record information about errors and other return options. No cap. π
β¨ NAME β¨
This page is serving all of these functions β yes, all of them, we are not skipping anybody:
Tcl_GetReturnOptionsTcl_SetReturnOptionsTcl_AddErrorInfoTcl_AppendObjToErrorInfoTcl_AddObjErrorInfoTcl_SetObjErrorCodeTcl_SetErrorCodeTcl_SetErrorCodeVATcl_SetErrorLineTcl_GetErrorLineTcl_PosixErrorTcl_LogCommandInfo
And the whole group-chat topic is literally retrieve or record information about errors and other return options. π«Ά
π SYNOPSIS π
Same signatures, same characters, absolutely zero edits. Copying this exactly is the assignment:
#include <tcl.h>
Tcl_Obj *
Tcl_GetReturnOptions(interp, code)
int
Tcl_SetReturnOptions(interp, options)
Tcl_AddErrorInfo(interp, message)
Tcl_AppendObjToErrorInfo(interp, objPtr)
Tcl_AddObjErrorInfo(interp, message, length)
Tcl_SetObjErrorCode(interp, errorObjPtr)
Tcl_SetErrorCode(interp, element, element, ... (char *)NULL)
Tcl_SetErrorCodeVA(interp, argList)
Tcl_GetErrorLine(interp)
Tcl_SetErrorLine(interp, lineNum)
const char *
Tcl_PosixError(interp)
void
Tcl_LogCommandInfo(interp, script, command, commandLength)
π¦ ARGUMENTS π¦
okay so here's the cast of characters, and yes every single one matters. πΈ
Tcl_Interp *interp(in)β the interpreter where you're recording information. That's the spot. π«Άint codeβ the code returned from script evaluation. iykyk. β¨Tcl_Obj *optionsβ a dictionary of return options. Lowkey a whole little bundle. πconst char *message(in)β forTcl_AddErrorInfo, this is a conventional C string to append to the-errorinforeturn option. ForTcl_AddObjErrorInfo, this points to the first byte of an array oflengthbytes containing a string to append to the-errorinforeturn option. This byte array may contain embedded null bytes unlesslengthis negative. Bestie, read that twice, it's important. πTcl_Obj *objPtr(in)β a message to be appended to the-errorinforeturn option in the form of aTcl_Objvalue. π¦int length(in)β the number of bytes to copy frommessagewhen appending to the-errorinforeturn option. If negative, all bytes up to the first null byte are used. That negative case is doing main character energy. πTcl_Obj *errorObjPtr(in)β the-errorcodereturn option will be set to this value. Periodt. πconst char *element(in)β string to record as one element of the-errorcodereturn option. Last element argument must be(char *)NULL. Do not forget that one. πva_list argList(in)β an argument list which must have been initialized usingva_start, and cleared usingva_end. Very "handle with care" of it. πint lineNumβ the line number of a script where an error occurred. πconst char *script(in)β pointer to first character inscriptcontainingcommand(must be<= command). Yes, that's the rule. πconst char *command(in)β pointer to first character incommandthat generated the error. Iconic error-origin pointer behavior. π₯int commandLength(in)β number of bytes incommand;-1means use all bytes up to first null byte. No cap. π
β¨ DESCRIPTION β¨
Okay bestie, so Tcl_SetReturnOptions and Tcl_GetReturnOptions are literally the C-interface glow-up of the return and catch commands. Same powers, different outfit. π
Tcl_GetReturnOptions goes and fetches the dictionary of return options out of an interpreter after a script has been evaluated. Routines like Tcl_Eval get called to evaluate a script in an interpreter β they hand back an integer completion code. They also leave behind, in the interpreter, both a result and a dictionary of return options that script evaluation generated. And just like Tcl_GetObjResult grabs the result, Tcl_GetReturnOptions grabs the dictionary of return options. Pass that integer completion code in as the code argument so all the required options actually show up in the dictionary. Specifically: a code value of TCL_ERROR guarantees entries for the keys -errorinfo, -errorcode, and -errorline appear in the dictionary. Also, the entries for the keys -code and -level get adjusted if necessary so they agree with the value of code. π«Ά
The (Tcl_Obj *) that Tcl_GetReturnOptions returns points at an unshared Tcl_Obj with a reference count of zero. You can write to that dictionary β add entries, remove entries, overwrite entries, whatever β with zero need to check whether the value is shared. And just like any Tcl_Obj with reference count zero, it's on you to arrange its disposal with Tcl_DecrRefCount, or to take a reference to it via Tcl_IncrRefCount (or one of the many functions that calls that, notably including Tcl_SetObjResult and Tcl_SetVar2Ex). π
A super typical use for Tcl_GetReturnOptions is pulling the stack trace when script evaluation returns TCL_ERROR, like so:
int code = Tcl_Eval(interp, script);
if (code == TCL_ERROR) {
Tcl_Obj *options = Tcl_GetReturnOptions(interp, code);
Tcl_Obj *key = Tcl_NewStringObj("-errorinfo", -1);
Tcl_Obj *stackTrace;
Tcl_IncrRefCount(key);
Tcl_DictObjGet(NULL, options, key, &stackTrace);
Tcl_DecrRefCount(key);
/* Do something with stackTrace */
Tcl_DecrRefCount(options);
}
Tcl_SetReturnOptions sets the return options of interp to be options. If options contains any invalid value for any key, TCL_ERROR gets returned and the interp result is set to an appropriate error message. Otherwise you get back a completion code in agreement with the -code and -level keys in options. β¨
As an example, Tcl's own return command could be implemented in terms of Tcl_SetReturnOptions like this:
if ((objc % 2) == 0) { /* explicit result argument */
objc--;
Tcl_SetObjResult(interp, objv[objc]);
}
return Tcl_SetReturnOptions(interp, Tcl_NewListObj(objc-1, objv+1));
Not me about to lie to you though β it is not really implemented that way. Internal access privileges allow for a more efficient alternative that meshes better with the bytecode compiler. π
Note that a newly created Tcl_Obj may be passed in as the options argument with no need to tend to any reference counting at all. This is analogous to Tcl_SetObjResult. π
So while Tcl_SetReturnOptions gives you a general interface for setting any collection of return options, there are a handful of return options that get used constantly. Most notably, the -errorinfo and -errorcode return options should be set properly whenever the command procedure of a command returns TCL_ERROR. The -errorline return option is also read by commands that evaluate scripts and wish to supply detailed error location information in the stack trace text they append to the -errorinfo option. Tcl provides several simpler interfaces to set these return options more directly. π
The -errorinfo option holds a stack trace of the operations that were in progress when an error occurred, and it's meant to be human-readable. The -errorcode option holds a Tcl list of items that are meant to be machine-readable. The first item in the -errorcode value identifies the class of error that occurred (e.g., POSIX means an error occurred in a POSIX system call), and additional elements hold additional pieces of information that depend on the class. See the manual entry on the errorCode variable for details on the various formats for the -errorcode option used by Tcl's built-in commands. π¦
The -errorinfo option value is gradually built up as an error unwinds through the nested operations. Every single time an error code is returned to Tcl_Eval, or any of the routines that performs script evaluation, the procedure Tcl_AddErrorInfo is called to add additional text to the -errorinfo value 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. Iconic behavior, honestly. β
It's sometimes useful to add additional information to the -errorinfo value beyond what the script evaluation routines can supply automatically. Tcl_AddErrorInfo may be used for exactly this purpose: its message argument is an additional string to be appended to the -errorinfo option. For example, when an error arises during the source command, the procedure Tcl_AddErrorInfo is called to record the name of the file being processed and the line number on which the error occurred. Likewise, when an error arises during evaluation of a Tcl procedures, the procedure name and line number within the procedure are recorded, and so on. The best time to call Tcl_AddErrorInfo is just after a script evaluation routine has returned TCL_ERROR. The value of the -errorline return option (retrieved via a call to Tcl_GetReturnOptions) often makes up a useful part of the message passed to Tcl_AddErrorInfo. πΈ
Tcl_AppendObjToErrorInfo is an alternative interface to the same functionality as Tcl_AddErrorInfo. You call Tcl_AppendObjToErrorInfo when the string value to be appended to the -errorinfo option is available as a Tcl_Obj instead of as a char array. π§
Tcl_AddObjErrorInfo is nearly identical to Tcl_AddErrorInfo, except it has an additional length argument. This lets the message string contain embedded null bytes. Which is essentially never a good idea. π If the message needs to contain the null character U+0000, Tcl's usual internal encoding rules should be used to avoid the need for a null byte. If the Tcl_AddObjErrorInfo interface is used at all, it should be with a negative length value. Periodt.
The procedure Tcl_SetObjErrorCode is used to set the -errorcode return option to the list value errorObjPtr built up by the caller. Tcl_SetObjErrorCode is typically invoked just before returning an error. If an error is returned without calling Tcl_SetObjErrorCode or Tcl_SetErrorCode, the Tcl interpreter automatically sets the -errorcode return option to NONE. π
The procedure Tcl_SetErrorCode is also used to set the -errorcode return option. However, it takes one or more strings to record instead of a value. Otherwise, it is similar to Tcl_SetObjErrorCode in behavior. π
Tcl_SetErrorCodeVA is the same as Tcl_SetErrorCode except that instead of taking a variable number of arguments it takes an argument list.
The procedure Tcl_GetErrorLine is used to read the integer value of the -errorline return option without the overhead of a full call to Tcl_GetReturnOptions. Likewise, Tcl_SetErrorLine sets the -errorline return option value. β‘
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 the -errorcode return option 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 the -errorcode value). It may be convenient to include this string as part of the error message returned to the application in the interpreter's result. π
Tcl_LogCommandInfo is invoked after an error occurs in an interpreter. It adds information about the command that was being executed when the error occurred to the -errorinfo value, and the line number stored internally in the interpreter is set. π
In older releases of Tcl, there was no Tcl_GetReturnOptions routine. In its place, the global Tcl variables errorInfo and errorCode were the only place to retrieve the error information. Much existing code written for older Tcl releases still accesses this information via those global variables. π
It is important to realize that while reading from those global variables remains a supported way to access these return option values, it is important not to assume that writing to those global variables will properly set the corresponding return options.
It has long been emphasized in this manual page that it is important to call the procedures described here rather than setting errorInfo or errorCode directly with Tcl_ObjSetVar2. No cap. π
ββοΈ
If the procedure Tcl_ResetResult is called, it clears all of the state of the interpreter associated with script evaluation, including the entire return options dictionary. In particular, the -errorinfo and -errorcode options are reset. If an error had occurred, the Tcl_ResetResult call will clear the error state to make it appear as if no error had occurred after all. The global variables errorInfo and errorCode are not modified by Tcl_ResetResult, so they continue to hold a record of information about the most recent error seen in an interpreter. ππ
π SEE ALSO π
Tcl_DecrRefCount(3)Tcl_IncrRefCount(3)Tcl_Interp(3)Tcl_ResetResult(3)Tcl_SetErrno(3)errorCode(n)errorInfo(n)
π KEYWORDS π
error, value, value result, stack, trace, variable
That's the whole page, bestie β go forth and set your return options the correct way, not the "assign to a global variable and hope" way. πβ¨
This little gem is from Tcl 8.5, and it's Tcl_AddErrorInfo(3) β catch you on the next eval, mwah π«Άπ
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl-devel 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1989-1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms