proc ucatch {body {_var ""}} {
if {$_var ne ""} {upvar 1 $_var var}
set code [catch {uplevel 1 $body} var]
if {$code == 1} {set var [u2x $var]}
return $code
}
proc u2x str {
set res ""
foreach c [split $str ""] {
scan $c %c int
append res [expr {$int>127? [format \\u%04X $int]: $c}]
}
set res
}#------------ Test: first with a regular catch % set a(\u1234) hello
% if [catch {puts $a(\u1235)} res] {puts $res}
can't read "a(?)": no such element in arrayNow comes the ucatch, admittedly more helpful: % if [ucatch {puts $a(\u1235)} res] {puts $res}
can't read "a(\u1235)": no such element in arrayVery clever, no? I beamed. But in came William of Occam, razor in hand. "Why three things, where two do the same job?", he asked. "How about % if [catch {puts $a(\u1235)} res] {puts [u2x $res]}?"I had to agree. Simplicity is sometimes more difficult than one expects...Category Debugging | Arts and crafts of Tcl-Tk programming

