Name

try_eval

try_eval code catch [finally]

Evaluate the command string code. If an error occurs, evaluate code and return the result. Last, execute the command string finally.

Example

try_eval {
        # code
        puts -nonewline stderr "Enter a number: "
        set ans [gets stdin]
        # could fail, e.g. due to divide by zero
        set res [expr 1.0 / $ans]
        puts stderr "1 / $ans = $res"
    } {
        # catch
        set msg [lindex $errorCode end]
        puts stderr "Error: $msg"
    } {
        # finally
        puts stderr "End of example"
    }

Get Tcl/Tk in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.