Version 0 of ucatch

Updated 2006-02-08 15:07:36 by suchenwi

Richard Suchenwirth 2006-02-08 - Debugging a Tcl script which deals with Unicodes can be difficult if you see error messages on stdout, but all characters not representable in your encoding system is converted to "?". In such a situation, I wrote the following catch deunicodifier:

 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 array

Now comes the ucatch, admittedly more helpful:

 % if [ucatch {puts $a(\u1235)} res] {puts $res}
 can't read "a(\u1235)": no such element in array


Category Debugging | Arts and crafts of Tcl-Tk programming