bgerror(n)
π bgerror(n) β the Tcl Built-In Commands glow-up π
okay bestie, pull up a chair, we're talking about bgerror β the command that catches your background errors so they don't just silently ruin your whole day. no cap. π«Ά
β¨ NAME β¨
bgerror β the command that gets invoked to process background errors π
π SYNOPSIS π
bgerror message
π¦ DESCRIPTION π¦
okay so here's the tea. Tcl Release 8.5 supports the interp bgerror command, and the way that works is it lets applications register, inside an interpreter, the exact command that's gonna handle background errors in that interpreter. very demure, very mindful. β¨
but in older releases of Tcl, that level of control simply did not exist π. applications could only control background-error handling by creating a command named bgerror in the global namespace of an interpreter. that was the whole vibe. and what follows in this doc describes the interface requirements of the bgerror command that an application might define to retain compatibility with pre-8.5 releases of Tcl.
π so if you're an application that only intends to support Tcl 8.5 and later, do not do all this. just make use of interp bgerror. periodt.
πΈ so what even IS bgerror πΈ
not me about to blow your mind: the bgerror command does not exist as a built-in part of Tcl. literally doesn't ship with it. instead, individual applications or users can define a bgerror command themselves (e.g. as a Tcl procedure) if they wish to handle background errors. it's giving DIY. π
π okay but what's a background error π
a background error is one that occurs in an event handler or some other command that did not originate with the application. for example, if an error occurs while executing a command specified with the after command, then it is a background error. slay, now you know. π
for a non-background error, the error can simply be returned up through nested Tcl command evaluations until it reaches the top-level code in the application β and then the application can report the error in whatever way it wishes. easy. iconic. no drama. β¨
but when a background error occurs, the unwinding ends in the Tcl library, and there is no obvious way for Tcl to report the error. awk. π that's the whole reason this command exists.
π what Tcl actually does when it detects one π
when Tcl detects a background error, it saves information about the error and invokes a handler command registered by interp bgerror later as an idle event handler. and the default handler command? it in turn calls the bgerror command. bestie chain of command fr fr. π«Ά
before invoking bgerror, Tcl restores the errorInfo and errorCode variables to their values at the time the error occurred, and then it invokes bgerror with the error message as its only argument. that's it. one arg. don't be extra.
Tcl assumes that the application has implemented the bgerror command, and that the command will report the error in a way that makes sense for the application. and here's the kicker β Tcl will ignore any result returned by the bgerror command as long as no error is generated. it doesn't care what you return. it's not reading your reply. π
β οΈ if another Tcl error occurs within thebgerrorcommand (for example, because nobgerrorcommand has been defined), then Tcl reports the error itself by writing a message tostderr. so like, define your command. please. π
π multiple errors? we handle those π
if several background errors accumulate before bgerror is invoked to process them, bgerror will be invoked once for each error, in the order they occurred. no skipping, no merging, in order, the way it should be.
but β plot twist β if bgerror returns with a break exception, then any remaining errors are skipped without calling bgerror. so you can tap out early if you want. π
π¨ a warning for the library girlies π¨
if you are writing code that will be used by others as part of a package or other kind of library, consider avoiding bgerror. the reason for this is that the application programmer may also want to define a bgerror, or use other code that does, and thus will have trouble integrating your code. it's giving conflict. it's giving friction. don't do that to them. π«Ά
β EXAMPLE β
this bgerror procedure appends errors to a file, with a timestamp. obsessed.
proc bgerror {message} {
set timestamp [clock format [clock seconds]]
set fl [open mylog.txt {WRONLY CREAT APPEND}]
puts $fl "$timestamp: bgerror in $::argv '$message'"
close $fl
}
π SEE ALSO π
π KEYWORDS π
- background error
- reporting
and that's the whole page bestie, you ate it up π
β¨ this was bgerror(n) from the Tcl Built-In Commands manual, Tcl version 7.5, dated 7.5 β mwah, go forth and handle your background errors responsibly, ily, bye ππ¦π
π¦ 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.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- centos-stream 10: tcl-doc 1:8.6.13-4.el10 Β· TCL AND GPL-3.0-or-later WITH bison-exception-2.2 AND BSD-3-Clause
- centos-stream 9: tcl-doc 1:8.6.10-7.el9 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1990-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