Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 591261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:36:59+00:00 2026-05-13T15:36:59+00:00

If using Tcl in interactive mode , in which I input the following: set

  • 0

If using Tcl in interactive mode , in which I input the following:

set list {1 2 3 4 5}
set sum 0
foreach el $list {
    set sum [expr $sum + $element]
}

it will show a piece of very terse info:

can't read "element": no such variable

but when I use

puts $errorInfo

it wil show:

can't read "element": no such variable
      while executing
"expr $sum + $element"
      ("foreach" body line 2)
      invoked from within
"foreach el $list {
      set sum [expr $sum + $element]
 }"

which is what I really want.

The problem is : in non-interactive mode when I want to catch this error and then puts the errorInfo to get a stack trace, it’ll merely display the terse info. How can I get the detailed stack trace like above? Thanks a lot!

Edited to add more details

say I have following code:

proc test1 {} {
    set list {1 2 3 4 5}
    set sum 0
    foreach el $list {
    if {[catch {set sum [expr $sum + $element]} err]} {
        puts $::errorInfo
    }
    break 
    }
}
proc test2 {} {
    foreach el $list {
    set list {1 2 3 4 5}
    set sum 0
    set sum [expr $sum + $element]
    }
}    
#test1
#test2

If I uncomment "#test1", it’ll show :

can't read "element": no such variable  
   while executing  
"expr $sum + $element"

if I uncomment "#test2", it’ll show:

can't read "element": no such variable  
    while executing
"expr $sum + $element"  
    (procedure "test2" line 5)  
    invoked from within  
"test2"  
    (file "./test.tcl" line 137)

What I want is of course the test2 behavior. How can I display this error info using catch?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:36:59+00:00Added an answer on May 13, 2026 at 3:36 pm

    Can you show how you catch/puts the information in non-interactive mode?

    If you did

    if {[catch {...your...code...here...} err]} {
       puts "Error info $err"
    }
    

    Then the behavior you described is expected – $err only has the “terse info”. What you might want to puts instead is:

       puts "Error info $err\nFull info: $::errorInfo"
    

    The prefix :: is required in case your catch is called inside a proc or namespace to ensure that the variable you use is the actual toplevel ::errorInfo.

    Edited to address the followup

    As Colin answered, the stack traces found in your test1 and test2 differ because of where you’ve placed the catch. Let me illustrate. Here are some chained Tcl procs:

    proc one {} {
      two
    }
    proc two {} {
      three
    }
    proc three {} {
      four
    }
    proc four {} {
      error "Yup, an error"
    }
    

    If you evaluate

    catch {four}
    puts $::errorInfo
    

    You will just get a stack trace that looks like this:

    Yup, an error
        while executing
    "error "Yup, an error""
        (procedure "four" line 2)
        invoked from within
    "four"
    

    This is because the stack trace between where the error happened (inside four) and where you caught it, there is only one procedure call.

    If instead you caught the error “further away” like so:

    catch {one}
    puts $::errorInfo
    

    The stack trace between the catch statement and the error includes the procs one, two, three, and four. This results in a stack trace like so:

    Yup, an error
        while executing
    "error "Yup, an error""
        (procedure "four" line 2)
        invoked from within
    "four"
        (procedure "three" line 2)
        invoked from within
    "three"
        (procedure "two" line 2)
        invoked from within
    "two"
        (procedure "one" line 2)
        invoked from within
    "one"
    

    So… to match your example for test1, if you redefined three as follows:

    proc three {} {
      catch {four}
      puts $::errorInfo
    }
    

    And you evaluated

    if {[catch {one}]} {
       puts "Found an error"
    }
    

    You would not see “Found an error”, because the body of three caught the error, and printed the stack trace. The stack trace only containing the calls between the catch statement and the error – which (like my first example) consists only of the call to four.

    So, where you place your catch statements matter.


    On a related note, you can re-throw the error, preserving the stack trace if you so desire. Here’s the new definition for three:

    proc three {} {
      if {[catch {four} err]} {
        puts "Caught an error $err, re-throwing"
        error $err $::errorInfo
      }
    }
    

    Now, with the re-thrown error, you would see this:

    tchsh% catch {one}
    Caught an error Yup, an error, re-throwing
    1
    tclsh% set ::errorInfo
    Yup, an error
        while executing
    "error "Yup, an error""
        (procedure "four" line 2)
        invoked from within
    "four"
        (procedure "three" line 2)
        invoked from within
    "three"
        (procedure "two" line 2)
        invoked from within
    "two"
        (procedure "one" line 2)
        invoked from within
    "one"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 315k
  • Answers 315k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have a web/iPhone app that renders a map for… May 13, 2026 at 11:16 pm
  • Editorial Team
    Editorial Team added an answer If you want to follow common patterns, expose a public… May 13, 2026 at 11:16 pm
  • Editorial Team
    Editorial Team added an answer Try this rule: RewriteEngine on RewriteRule ^favicon\.ico$ /images/favicon.ico [L] Edit    And… May 13, 2026 at 11:16 pm

Related Questions

I need to have two way communication between threads in Tcl and all I
I have a c++ server side project that I need to embed some sort
Using SQLite I need to copy nearly all of an existing row from a
I have some TCL scripts on the server . I am using a client
I'm using visual studio to program this small TcpServer. It's really specific. The server

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.