password636
password636

Reputation: 1011

tcl catch: different -code and -level in caught result options when return code is TCL_RETURN

Please take a look at the following code snippet:

set catch_r [catch {    
    return -code return -level 0 "something"    
    #return -code break -level 1 "something"
} result options]
puts "catch result: \n$catch_r"
puts result:
puts $result
puts options:
puts $options

the output is

catch result:
2
result:
something
options:
-code 0 -level 1

If I use the commented line return -code break -level 1 "something", the output is

catch result:
2
result:
something
options:
-code 3 -level 1

I tried more return -code -level, such as -code continue or -code error with different -level numbers, and found that values of -code and -level in options are the same as return -code -level if it is NOT return -code return.

If it's return -code return -level N, options will be -code 0 -level N+1, not -code 2 -level N. Why return -code return is treated this way by catch? What's the meaning of it?

Thanks

Upvotes: 0

Views: 115

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13282

Every kind of return -code X gives TCL_RETURN until it has gone through as many stack levels as the value of the -level options. Then it gives the TCL_ code that corresponds to its -code value. For -code return that is TCL_RETURN, which means it will go through one more stack level and give TCL_OK. As you write, return -code return -level N is the same as return -code ok -level N+11. That is by design.

Documentation: return

1 pseudocode: the command will not actually do level arithmetic.

Upvotes: 1

Related Questions