Purpose: to discuss the variety of coding techniques available in Tcl for handling errors. ---- There are a number of types of errors that Tcl programs can encounter. I am going to write about one particular one - but Tcl'ers should feel free to add information about handling other types of errors as they have time. One of the more annoying ''errors'' Tcl programers encounter occurs when a program being exec'd terminates with a non-zero exit code. Tcl by default considers that an error condition that should terminate the program. However, there are various programs whose exit codes are in some way significant. One of the first a Tcl programmer usually encounters is grep, the general regular expression printer, which searches through an input file looking for patterns and outputting the lines which match. The exit code from grep indicates either an error, no lines matched, or success. In this article[http://www.deja.com/getdoc.xp?AN=497174424]: From: claird@Starbase.NeoSoft.COM (Cameron Laird) Subject: Re: grep Date: 4 Jul 1999 17:24:32 -0500 Message-ID: <7lomv0$s2q$1@Starbase.NeoSoft.COM> Cameron provides us with a tcl idiom for invoking grep or other commands which terminate with non-zero return codes. if [catch {exec grep $pattern $file} result] { puts "We found nothing." } else { puts "We found $result." } In real usage, one put replace the "We found nothing" with code to handle the error condition handling you would want to occur and the other puts would be replaced by what you wanted to occur when success occurs. '''WARNING!''' The above idiom doesn't indicate what will happen to the command's error messages. This all depends on the command itself. A program executing a command which writes errors to a console or the current tty will find the output appearing. Grep on some platforms writes its error to stderr - which means that neither the program nor the user will see it. ---- If you have a look at the global variable ''errorCode'' when the catch command above indicates that an error was caught, you will find all sorts of useful info available. If the first word of the list contained within was ''CHILDSTATUS'' then you know that the command you ran (grep in this case) exited with some indication of a failure code. The most common case where this happens in our example is when the pattern doesn't exist in the file. If the first word of the list contained within was ''NONE'' (in fact that is the whole of the list) then you know that there was no non-zero exit status - the error is purely due to the fact that some output was generated on stderr. There are several other possible error codes; see the tclvars(n) manpage for more details. '''DKF''' ----