bgerror(3tcl)

πŸ’– bgerror β€” the Tcl built-in that handles your background errors, bestie πŸ’–

okay so you're poking around the bgerror man page from Tcl's Built-In Commands and you need the tea? say less. this is the whole thing, no skips, no cap. ✨


✨ NAME ✨

bgerror β€” the command that gets invoked to process background errors 🫢


🌸 SYNOPSIS 🌸

bgerror message

πŸ’… DESCRIPTION πŸ’…

so like, plot twist: Release 8.5 of Tcl supports the interp bgerror command, which lets applications register, inside an interpreter, the command that's gonna handle background errors in that interpreter. iconic, honestly. but in older releases of Tcl, that level of control just... wasn't available 😭 and applications could only control the handling of background errors by creating a command with the particular command name bgerror in the global namespace of an interpreter.

the docs below describe the interface requirements of the bgerror command an application might define to stay compatible with pre-8.5 releases of Tcl. if your app only needs to support Tcl releases 8.5 and later, just use interp bgerror and move on with your life, periodt. πŸ’…

lowkey important: the bgerror command does not exist as a built-in part of Tcl. instead, individual applications or users can define a bgerror command (e.g. as a Tcl procedure) if they wish to handle background errors. it's giving DIY. πŸŽ€

so what even is a background error? bestie. 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. 🌈

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; then the application can report the error in whatever way it wishes. 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. πŸ’€

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. the default handler command in turn calls the bgerror command. before invoking bgerror, Tcl restores the errorInfo and errorCode variables to their values at the time the error occurred, then it invokes bgerror with the error message as its only argument. 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. Tcl will ignore any result returned by the bgerror command as long as no error is generated. πŸ™ˆ

now β€” if another Tcl error occurs within the bgerror command (for example, because no bgerror command has been defined) then Tcl reports the error itself by writing a message to stderr. yeah. it's just gonna yell into the void for you. 😭

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. however, if bgerror returns with a break exception, then any remaining errors are skipped without calling bgerror. bye bestie, we're done here. πŸ‘‘

⚠️ real talk: 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. not a vibe. don't be that friend. 🫢

πŸ¦‹ EXAMPLE πŸ¦‹

this bgerror procedure appends errors to a file, with a timestamp. slay.

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 β€” this one's from the Tcl Built-In Commands manual, Tcl version 7.5, and it's serving bgerror(3tcl) realness. go forth and handle those background errors with main character energy. πŸ’œβœ¨πŸŽ€