davidbessire
Programmer
If the script i use in a catch:
a. is not wrapped in "proc"; and,
b. ends with "return"
Then:
catch considers this an error (i.e. not TCL_OK).
This feature is documented in the Tcl/Tk man pages and in Welch.
As an example of the behavior, consider:
% proc bar { } { return }
% catch { bar }
0
% catch { return }
2
Question: How do you write code that employs Tcl's dynamic script construction & execution capabilities without risking this spurious error? This behavior prevents writing something like ...
if { [catch {
# complicated script with several return paths
} error] } {
# report error
}
... unless you convert the script into a procedure. Otherwise, *any* return from the script via "return" will trigger error handling (even "return -code 0"). Procs using "catch" and "uplevel" to run untrusted & potentially buggy scripts (perhaps as part of new control structures) may generate spurious stack traces.
Any way to avoid the inelegance below?
set failed [catch {$code} error]
if { $failed == 1 } {
# handle TCL_ERROR
} elseif { $failed == 2 } {
# handle TCL_RETURN
} elseif { $failed == 3 } {
# handle TCL_BREAK
} elseif { $failed == 4 } {
# handle TCL_CONTINUE
}
Thanks,
davidbessire
a. is not wrapped in "proc"; and,
b. ends with "return"
Then:
catch considers this an error (i.e. not TCL_OK).
This feature is documented in the Tcl/Tk man pages and in Welch.
As an example of the behavior, consider:
% proc bar { } { return }
% catch { bar }
0
% catch { return }
2
Question: How do you write code that employs Tcl's dynamic script construction & execution capabilities without risking this spurious error? This behavior prevents writing something like ...
if { [catch {
# complicated script with several return paths
} error] } {
# report error
}
... unless you convert the script into a procedure. Otherwise, *any* return from the script via "return" will trigger error handling (even "return -code 0"). Procs using "catch" and "uplevel" to run untrusted & potentially buggy scripts (perhaps as part of new control structures) may generate spurious stack traces.
Any way to avoid the inelegance below?
set failed [catch {$code} error]
if { $failed == 1 } {
# handle TCL_ERROR
} elseif { $failed == 2 } {
# handle TCL_RETURN
} elseif { $failed == 3 } {
# handle TCL_BREAK
} elseif { $failed == 4 } {
# handle TCL_CONTINUE
}
Thanks,
davidbessire