1bgerror(n)                   Tcl Built-In Commands                  bgerror(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       bgerror - Command invoked to process background errors
9

SYNOPSIS

11       bgerror message
12_________________________________________________________________
13
14

DESCRIPTION

16       The  bgerror  command  doesn't exist as built-in part of Tcl.  Instead,
17       individual applications or users can define a bgerror command (e.g.  as
18       a Tcl procedure) if they wish to handle background errors.
19
20       A background error is one that occurs in an event handler or some other
21       command that didn't originate with the application.  For example, if an
22       error  occurs  while  executing a command specified with the after com‐
23       mand, then it is a background error.  For a non-background  error,  the
24       error  can simply be returned up through nested Tcl command evaluations
25       until it reaches the top-level code in the application; then the appli‐
26       cation  can  report  the error in whatever way it wishes.  When a back‐
27       ground error occurs, the unwinding ends in the Tcl library and there is
28       no obvious way for Tcl to report the error.
29
30       When  Tcl  detects  a  background error, it saves information about the
31       error and invokes the bgerror command later as an idle  event  handler.
32       Before invoking bgerror, Tcl restores the errorInfo and errorCode vari‐
33       ables to their values at the time the error occurred, then  it  invokes
34       bgerror  with the error message as its only argument.  Tcl assumes that
35       the application has implemented the bgerror command, and that the  com‐
36       mand  will  report the error in a way that makes sense for the applica‐
37       tion.  Tcl will ignore any result returned by the  bgerror  command  as
38       long as no error is generated.
39
40       If  another  Tcl  error occurs within the bgerror command (for example,
41       because no bgerror command has been defined) then Tcl reports the error
42       itself by writing a message to stderr.
43
44       If  several  background  errors accumulate before bgerror is invoked to
45       process them, bgerror will be invoked once for each error, in the order
46       they  occurred.   However,  if  bgerror returns with a break exception,
47       then any remaining errors are skipped without calling bgerror.
48
49       Tcl has no default implementation for bgerror. However, in applications
50       using  Tk there is a default bgerror procedure which posts a dialog box
51       containing the error message and offers the user  a  chance  to  see  a
52       stack  trace showing where the error occurred.  In addition to allowing
53       the user to view the stack trace, the  dialog  provides  an  additional
54       application configurable button which may be used, for example, to save
55       the stack trace to a file.  By default, this is the behavior associated
56       with that button.  This behavior can be redefined by setting the option
57       database values *ErrorDialog.function.text, to specify the caption  for
58       the  function button, and *ErrorDialog.function.command, to specify the
59       command to be run.  The text of the stack trace is appended to the com‐
60       mand  when  it  is evaluated.  If either of these options is set to the
61       empty string, then the additional button will not be displayed  in  the
62       dialog.
63
64       If  you are writing code that will be used by others as part of a pack‐
65       age or other kind of library, consider avoiding  bgerror.   The  reason
66       for  this  is that the application programmer may also want to define a
67       bgerror, or use other code that does and thus will have  trouble  inte‐
68       grating your code.
69

EXAMPLE

71       This bgerror procedure appends errors to a file, with a timestamp.
72              proc bgerror {message} {
73                  set timestamp [clock format [clock seconds]]
74                  set fl [open mylog.txt {WRONLY CREAT APPEND}]
75                  puts $fl "$timestamp: bgerror in $::argv '$message'"
76                  close $fl
77              }
78
79

SEE ALSO

81       after(n), tclvars(n)
82
83

KEYWORDS

85       background error, reporting
86
87
88
89Tcl                                   7.5                           bgerror(n)
Impressum