How can I verify that the Tcl command did not throw an exception or error?

I have the following procedure:

proc myexec { args } { info_msg "Executing shell command: $args" set catch_res [catch {eval exec $args} res] if { $catch_res != 0 } { error_msg "Failed in command: $args" error_msg "$res" } return $catch_res } 

It only checks Unix commands and does not work with Tcl commands. How can I change this procedure to work with Tcl commands?

For example, if I want to verify that the following commands are evaluated without errors:

 set object1 $object2 

or

 open $filename r 
+4
source share
2 answers

The easiest way is to do this:

 proc myeval { args } { info_msg "Executing Tcl command: $args" set catch_res [catch $args res] if { $catch_res != 0 } { error_msg "Failed in command: $args" error_msg "$res" } return $res } 

Here we have replaced catch {eval exec $args} res with catch $args res (plus some cosmetic stuff), which will make the arguments evaluate to script without further substitution. You would use it as follows:

 myeval foo bar $boo 

Alternatively, if you are doing substitutions in catch , you should better write this more complex version:

 proc myeval { script } { info_msg "Executing Tcl command: [string trim $script]" set catch_res [catch [list uplevel 1 $script] res] if { $catch_res != 0 } { error_msg "Failed in command: [string trim $script]" error_msg "$res" } return $res } 

In this case, you would call it like this:

 myeval { foo bar $boo } 
+3
source

If you just want to change this procedure so that it runs the arguments passed as a Tcl command, and not as a Unix command, just release exec , i.e. instead of eval exec $args just eval $args .

Creating a handle to a single procedure for both Tcl and Unix commands is more complicated, but Tcl already does this for commands that you type in tclsh or want to - it first looks for a built-in command, and if it does not find it, it tries to run it as a Unix command . You can enable this behavior in the script by running set ::tcl_interactive 1 - more in the Tcl Wiki

+1
source

All Articles