Tcl_BackgroundError(3tcl)
π Tcl_BackgroundException, Tcl_BackgroundError β reporting the Tcl exception that popped off during background processing π
okay bestie, this one lives in the Tcl library procedures (man page Tcl_BackgroundError(3tcl), hey girl hey) and it's literally all about what to do when your background code goes "oop π" and nobody's around to hear it.
β¨ SYNOPSIS β¨
#include <tcl.h>
Tcl_BackgroundException(interp, code)
Tcl_BackgroundError(interp)
πΈ ARGUMENTS πΈ
Tcl_Interp *interp(in) β the interpreter where the exception happened, like the crime scene π΅οΈββοΈint code(in) β the exceptional return code you're reporting, aka the receipts π
π DESCRIPTION π
so here's the vibe. You call this procedure when a Tcl exception β and by that we mean any return code that isn't TCL_OK β happens during "background processing", like when an event handler is doing its thing. When that exception hits, the condition gets reported to Tcl or to a widget or to some other C code, and usually that code has no obvious way to tell the user about it. Awkward. π
That's where you come in: the code calls Tcl_BackgroundException with an interp argument that identifies the interpreter where the exception occurred, and a code argument holding the return code value of the exception. Then β and this is the iconic part β the state of the interpreter gets captured and saved, including any error message sitting in the interpreter result, plus the values of any entries in the return options dictionary. β¨
Then Tcl_BackgroundException arranges for the event loop to invoke, at some later time, the command registered in that interpreter to handle background errors via the interp bgerror command β and it passes the captured values as arguments. That registered handler command is meant to report the exception in whatever way makes sense for your application, main character style. π
The handler command receives two arguments: the result of the interp, and the return options of the interp at the time the error occurred.
- If the application registers no handler command, the default handler command will attempt to call
bgerrorto report the error. - If an error condition arises while invoking the handler command, then
Tcl_BackgroundExceptionreports the error itself by printing a message on the standard error file. (not the glow-up we wanted, but ok π)
lowkey important: Tcl_BackgroundException does not invoke the handler command immediately, because that could potentially interfere with scripts that are in process at the time the error occurred. Instead, it invokes the handler command later as an idle callback. patience is a virtue, bestie π§
It is possible for many background exceptions to accumulate before the handler command is invoked. When that happens, each of the exceptions is processed in order, one by one, no cutting the line. π But heads up: if the handler command returns a break exception, then all remaining error reports for the interpreter are skipped. bye felicia π
π¦ the older, simpler one
The Tcl_BackgroundError routine is an older and simpler interface, useful when the exception code reported is TCL_ERROR. It is equivalent to:
Tcl_BackgroundException(interp, TCL_ERROR);
that's it, that's the whole function, she's short and she serves π
β KEYWORDS β
background, bgerror, error, interp
and that's the whole page, periodt. this one's from the Tcl library docs, tagged Tcl 7.5 and dated 7.5 β go off, background error handling, you ate π«Άπ
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1992-1994 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms