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

  1.  
  2. # 'catch' and 'try' should behave the same.
  3. # With 'yieldto return -code error', they apparently don't.
  4. # Any ideas?
  5. # Or just something so obvious I can't see it?
  6.  
  7. proc wrapper args {
  8. yield
  9. yieldto return -code error "error from [info coroutine] with $args"
  10. }
  11.  
  12. proc test-with-catch {} {
  13. puts "-- [info level 0]"
  14. coroutine CORO wrapper ...]
  15.  
  16. puts "before yieldto return -code error ..."
  17. if {[catch {CORO zwei} err opt]} {
  18. puts "\terr=$err"
  19. puts "\topt=$opt"
  20. }
  21.  
  22. puts "info commands CORO=<[info commands CORO]>"
  23. }
  24.  
  25. proc test-with-try {} {
  26. puts "-- [info level 0]"
  27. coroutine CORO wrapper ...
  28.  
  29. puts "before yieldto return -code error ..."
  30. try {
  31. CORO zwei
  32. } on error {err opt} {
  33. puts "\terr=$err"
  34. puts "\topt=$opt"
  35. }
  36.  
  37. puts "info commands CORO=<[info commands CORO]>"
  38. }
  39.  
  40. test-with-catch
  41. puts ""
  42. try {test-with-try} on error {err opt} {
  43. puts "ERROR: $err"
  44. puts "OPT: $opt"
  45. }
  46.  
  47. # running the code above gives the following output:
  48. #
  49. # -- test-with-catch
  50. # before yieldto return -code error ...
  51. # err=error from ::CORO with ...\]
  52. # opt=-code 1 -level 1 -errorcode NONE
  53. # info commands CORO=<CORO>
  54. #
  55. # -- test-with-try
  56. # before yieldto return -code error ...
  57. # ERROR: error from ::CORO with ...
  58. # OPT: -errorcode NONE -code 1 -level 0 -errorstack {INNER {invokeStk1 test-with-try}} -errorinfo {error from ::CORO with ...
  59. # while executing
  60. # "test-with-try"
  61. # ("try" body line 1)} -errorline 1
  62.  
  63.  

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