Version 1 of exec and error information

Updated 2003-02-27 11:50:24

Arjen Markus (27 february 2003) The exec command will return an error whenever the program (or process) that was invoked writes to standard error (or exists with a non-zero value). Some programs, like compilers, use this output channel not only to report errors but to report progress as well.

A naive script like this:

   exec f77 -o myprog myprog.f

fails (at least on Sun/Solaris), simply because the compiler, the now venerable Fortran 77 compiler, writes the names of the subroutines it encounters to standard-error.

You can use catch to prevent the script from failing, but if there truly is an error in the compilation process, how do you know?

Well, that is where the Tcl standard variables errorCode and errorInfo come in. They are described in extenso in the man page on tclvars. But here is an example:

 # Attempt to catch the return code from f77
 #
 set rc [catch { exec f77 -c myff2.f } msg ]
 set errc $errorCode; set erri $errorInfo
 puts "rc: $rc"
 puts "errc: $errc"
 puts "erri: $erri"
 puts "msg: $msg"

The file "myff2.f" does not exist and here is the result:

 rc: 1
 errc: CHILDSTATUS 7612 1
 erri: myff2.f:
 Error: Cannot open file myff2.f 

     while executing
 "exec f77 -c myff2.f "
 msg: myff2.f:
 Error: Cannot open file myff2.f 

Changing the script (to compile the existing file "myff.f") gives:

 rc: 1
 errc: NONE
 erri: myff.f:
         myff:
     while executing
 "exec f77 -c myff.f "
 msg: myff.f:
         myff:

Note that the return value from catch is 1 in both cases, but the big difference is in errorCode.


[ Arts and crafts of Tcl-Tk programming ]