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