bgerror

bgerror is the name of a command that, if it exists, is invoked when an error occurs during the evaluation of command under the Tcl event loop. bgerror was superceded in Tcl 8.5 by interp bgerror, but for the purpose of backwards compatibility current versions of Tcl still support the bgerror mechanism.

Synopsis

bgerror message

Documentation

man page

Description

"bgerror" is the name of the procedure that gets called to handle background errors, i.e., errors that can not be propagated normally up the stack.

It is not expected that user code will call [bgerror] directly. Instead, it is there to be redefined so that user code can monitor and respond to errors in event handlers.

In very old code you may see this command written as tkerror; that name is obsolete nowadays. Use bgerror instead as it has the same semantics but without the implicit coupling to Tk.

Example

During development, one might rush a hack like

proc bgerror message {
    puts stderr "An event-based script faulted with '$message'."
}

into use.

DKF: I find that it's better to print errorInfo in such cases.

bgerror and tile in Tcl 8.4

KPV 2008-06-13 : In 8.4 the default bgerror code is not tile aware. Thus, the following idiomatic (but incorrect) code will fail (more precisely, the bgerror code configures a scrollbar's -relief which tile doesn't allow):

package require -exact Tk 8.4
package require tile
namespace import -force ::ttk::*
after 10 asdf

Does the documentation describe the calling convention? I didn't notice it. My experience is that the error-handler invokes it as something like

bgerror [firstLineOf $::errorInfo]

Ken: I want to enquire what is the best method if i got a proc that runs every 20s like the follow code but i want to catch its errors if any of the proc that run afterwards run into errors? I heard using bgerror is not recommended, so what is the best method?

proc a { } {
    #do something...
    after 1000 a 
}