Skip to content
Tcl Source Code Artifact [57f9975100]
Login

Artifact [57f9975100]

Artifact 57f99751004560536fd34e2db48eb30c119598887d4d0ae6dd54ce385b13c4a5:



CommandName: bgerror ManualSection: n Version: 7.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:

Name

bgerror - Command invoked to process background errors

Synopsis

::: {.synopsis} ::: [bgerror]{.cmd} [message]{.arg} :::

Description

Release 8.5 of Tcl supports the fBinterp bgerrorfR command, which allows applications to register in an interpreter the command that will handle background errors in that interpreter. In older releases of Tcl, this level of control was not available, and applications could control the handling of background errors only by creating a command with the particular command name fBbgerrorfR in the global namespace of an interpreter. The following documentation describes the interface requirements of the fBbgerrorfR command an application might define to retain compatibility with pre-8.5 releases of Tcl. Applications intending to support only Tcl releases 8.5 and later should simply make use of fBinterp bgerrorfR.

The fBbgerrorfR command does not exist as built-in part of Tcl. Instead, individual applications or users can define a fBbgerrorfR command (e.g. as a Tcl procedure) if they wish to handle background errors.

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 fBafterfR 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. 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 fBinterp bgerrorfR later as an idle event handler. The default handler command in turn calls the fBbgerrorfR command . Before invoking fBbgerrorfR, Tcl restores the fBerrorInfofR and fBerrorCodefR variables to their values at the time the error occurred, then it invokes fBbgerrorfR with the error message as its only argument. Tcl assumes that the application has implemented the fBbgerrorfR 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 fBbgerrorfR command as long as no error is generated.

If another Tcl error occurs within the fBbgerrorfR command (for example, because no fBbgerrorfR command has been defined) then Tcl reports the error itself by writing a message to stderr.

If several background errors accumulate before fBbgerrorfR is invoked to process them, fBbgerrorfR will be invoked once for each error, in the order they occurred. However, if fBbgerrorfR returns with a break exception, then any remaining errors are skipped without calling fBbgerrorfR.

If you are writing code that will be used by others as part of a package or other kind of library, consider avoiding fBbgerrorfR. The reason for this is that the application programmer may also want to define a fBbgerrorfR, or use other code that does and thus will have trouble integrating your code.

Example

This fBbgerrorfR procedure appends errors to a file, with a timestamp.

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
}