Posted to tcl by kbk at Sat Mar 15 17:01:00 GMT 2025view raw

  1. proc doit {script} {
  2. set code [catch {uplevel 1 $script} result opts]
  3. dict incr opts -level; # repeat any exception in the caller
  4. return -options $opts $result
  5. }
  6.  
  7. puts "Normal operation"
  8. for {set i 0} {$i < 3} {incr i} {
  9. doit {puts $i}
  10. }
  11. puts "Break"
  12. for {set i 0} {$i < 5} {incr i} {
  13. doit {if {$i > 3} break; puts $i}
  14. }
  15. puts "Continue"
  16. for {set i 0} {$i < 5} {incr i} {
  17. doit {if {$i < 3} continue; puts $i}
  18. }
  19. puts "Return"
  20. proc tryit {} {
  21. doit {return {it worked}}
  22. }
  23. puts [tryit]
  24. puts "Stack trace of errors:"
  25. proc fail {} {
  26. doit {error {Test an error} {Test an error} {MYERROR}}
  27. }
  28. set status [catch [fail] result]
  29. puts "status = $status"
  30. puts "result = $result"
  31. puts "stacktrace = $::errorInfo"
  32.