Posted to tcl by nnuio at Fri Jul 20 13:12:55 GMT 2018view pretty

# 'catch' and 'try' should behave the same.
# With 'yieldto return -code error', they apparently don't.
# Any ideas?
# Or just something so obvious I can't see it?

proc wrapper args {
    yield
    yieldto return -code error "error from [info coroutine] with $args"
}

proc test-with-catch {} {
    puts "-- [info level 0]"
    coroutine CORO wrapper ...]

    puts "before yieldto return -code error ..."
    if {[catch {CORO zwei} err opt]} {
        puts "\terr=$err"
        puts "\topt=$opt"
    }

    puts "info commands CORO=<[info commands CORO]>"
}

proc test-with-try {} {
    puts "-- [info level 0]"
    coroutine CORO wrapper ...

    puts "before yieldto return -code error ..."
    try {
        CORO zwei
    } on error {err opt} {
        puts "\terr=$err"
        puts "\topt=$opt"
    }

    puts "info commands CORO=<[info commands CORO]>"
}

test-with-catch
puts ""
try {test-with-try} on error {err opt} {
    puts "ERROR: $err"
    puts "OPT: $opt"
}

# running the code above gives the following output:
#
# -- test-with-catch
# before yieldto return -code error ...
# 	err=error from ::CORO with ...\]
# 	opt=-code 1 -level 1 -errorcode NONE
# info commands CORO=<CORO>
# 
# -- test-with-try
# before yieldto return -code error ...
# ERROR: error from ::CORO with ...
# OPT: -errorcode NONE -code 1 -level 0 -errorstack {INNER {invokeStk1 test-with-try}} -errorinfo {error from ::CORO with ...
#     while executing
# "test-with-try"
#     ("try" body line 1)} -errorline 1

Comments

Posted by nnuio at Fri Jul 20 13:29:00 GMT 2018 [text] [code]

from the Tcl chat: [15:19] <aspect> nnuio: your coro is yielding to return, so [try .. on error] doesn't catch that. [try .. on return] would. [15:21] <aspect> changing it to [yieldto return -level 0 -code error ...] should do what you want