| 1 |
'\" |
| 2 |
'\" Copyright (c) 1990-1994 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH bgerror n 7.5 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
bgerror \- Command invoked to process background errors |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBbgerror \fImessage\fR |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
Release 8.5 of Tcl supports the \fBinterp bgerror\fR command, |
| 20 |
which allows applications to register in an interpreter the command |
| 21 |
that will handle background errors in that interpreter. In older |
| 22 |
releases of Tcl, this level of control was not available, and applications |
| 23 |
could control the handling of background errors only by creating |
| 24 |
a command with the particular command name \fBbgerror\fR in the |
| 25 |
global namespace of an interpreter. The following documentation |
| 26 |
describes the interface requirements of the \fBbgerror\fR command |
| 27 |
an application might define to retain compatibility with pre-8.5 |
| 28 |
releases of Tcl. Applications intending to support only |
| 29 |
Tcl releases 8.5 and later should simply make use of \fBinterp bgerror\fR. |
| 30 |
.PP |
| 31 |
The \fBbgerror\fR command does not exist as built-in part of Tcl. Instead, |
| 32 |
individual applications or users can define a \fBbgerror\fR |
| 33 |
command (e.g. as a Tcl procedure) if they wish to handle background |
| 34 |
errors. |
| 35 |
.PP |
| 36 |
A background error is one that occurs in an event handler or some |
| 37 |
other command that did not originate with the application. |
| 38 |
For example, if an error occurs while executing a command specified |
| 39 |
with the \fBafter\fR command, then it is a background error. |
| 40 |
For a non-background error, the error can simply be returned up |
| 41 |
through nested Tcl command evaluations until it reaches the top-level |
| 42 |
code in the application; then the application can report the error |
| 43 |
in whatever way it wishes. When a background error occurs, the |
| 44 |
unwinding ends in the Tcl library and there is no obvious way for Tcl |
| 45 |
to report the error. |
| 46 |
.PP |
| 47 |
When Tcl detects a background error, it saves information about the |
| 48 |
error and invokes a handler command registered by \fBinterp bgerror\fR |
| 49 |
later as an idle event handler. The default handler command in turn |
| 50 |
calls the \fBbgerror\fR command . |
| 51 |
Before invoking \fBbgerror\fR, Tcl restores the |
| 52 |
\fBerrorInfo\fR and \fBerrorCode\fR variables to their values at the |
| 53 |
time the error occurred, then it invokes \fBbgerror\fR with the error |
| 54 |
message as its only argument. Tcl assumes that the application has |
| 55 |
implemented the \fBbgerror\fR command, and that the command will |
| 56 |
report the error in a way that makes sense for the application. Tcl |
| 57 |
will ignore any result returned by the \fBbgerror\fR command as long |
| 58 |
as no error is generated. |
| 59 |
.PP |
| 60 |
If another Tcl error occurs within the \fBbgerror\fR command (for |
| 61 |
example, because no \fBbgerror\fR command has been defined) then Tcl |
| 62 |
reports the error itself by writing a message to stderr. |
| 63 |
.PP |
| 64 |
If several background errors accumulate before \fBbgerror\fR is |
| 65 |
invoked to process them, \fBbgerror\fR will be invoked once for each |
| 66 |
error, in the order they occurred. However, if \fBbgerror\fR returns |
| 67 |
with a break exception, then any remaining errors are skipped without |
| 68 |
calling \fBbgerror\fR. |
| 69 |
.PP |
| 70 |
If you are writing code that will be used by others as part of a |
| 71 |
package or other kind of library, consider avoiding \fBbgerror\fR. |
| 72 |
The reason for this is that the application programmer may also want |
| 73 |
to define a \fBbgerror\fR, or use other code that does and thus will |
| 74 |
have trouble integrating your code. |
| 75 |
.SH "EXAMPLE" |
| 76 |
.PP |
| 77 |
This \fBbgerror\fR procedure appends errors to a file, with a timestamp. |
| 78 |
.PP |
| 79 |
.CS |
| 80 |
proc bgerror {message} { |
| 81 |
set timestamp [clock format [clock seconds]] |
| 82 |
set fl [open mylog.txt {WRONLY CREAT APPEND}] |
| 83 |
puts $fl "$timestamp: bgerror in $::argv '$message'" |
| 84 |
close $fl |
| 85 |
} |
| 86 |
.CE |
| 87 |
.SH "SEE ALSO" |
| 88 |
after(n), errorCode(n), errorInfo(n), interp(n) |
| 89 |
.SH KEYWORDS |
| 90 |
background error, reporting |
| 91 |
'\" Local Variables: |
| 92 |
'\" mode: nroff |
| 93 |
'\" End: |