> set catchReturn \
>   [catch {
>     return "my return value"
>    } catchResult]
> puts "catch return is: $catchReturn"
> puts "script result is: $catchResult"
>
> spits out:
>
> catch return is: 2
> script result is: my return value
>
> ?? catch should return 0 if the script doesn't exit with an error, correct?

Nope.  The "catch" command's value (which you're putting in catchReturn)
is the return code of the script it executed.  The last command in that
script was a "return" command, which always returns code 2
(aka TCL_RETURN).  [See postscript.]

If you want catch's script to return code 0, try something like this:

    set catchReturn [catch {
        set unused_variable "my return value"
    } catchResult]

The "set" command's value is the value of the variable, and its return
code is 0 (in this case).

Of course, this sets a useless variable.  If you're computing the value
some other way, you can just let the computation be the last statement
of the catch script.  For example:

    set catchReturn [catch {
        string tolower "My Return Value"
    } catchResult]

    # catchReturn == 0
    # catchResult == "my return value"

PS. You might be thinking that "return -code N" makes the "return"
command return code N. It doesn't.

Reply via email to