Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Errors in Packages

Status
Not open for further replies.

ipz8

Programmer
Jan 10, 2007
4
US
Hello everyone,

I have a problem with a package that I am creating. Basically I wanted to capture all errors with the error keyword. Unfortunately after triggering an error once while using the package, I can't trigger the same error again. I was wondering if anyone could let me know a bit more on this behavior.

As a side note in my regular scripts, errors are working just as I am expecting (where I can get multiple errors).

As another side note, why aren't arrays allowed in a package?
 
I presume you're creating a package with Tcl scripts (and not with C code).

If you can't get twice the same result, yoour script is not in the same state the second time. Did you tried your scripts with a console and left some badly initialized variable?

As a side note: did you considered to catch all the package generated errors forwarding them thru return -code code mesage?
(more at & ).

I can't understand why you can't use arrays in your package (I use them in my proper packages). Can you say more about that?

HTH

ulis
 
Thank you Ulis for responding.

First I found out my problem with the error command. I just don't understand why the TCL interpreter acts weird after the first error gets thrown that is uncaught. Can you explain what happens?

I also tried to add just a simple array with a few elements as a variable in my package. Something like...

namespace eval ::MyPackage {

# Important Variables
variable my_user(1)
variable my_user(2)
}
 
[red]I just don't understand why the TCL interpreter acts weird after the first error gets thrown that is uncaught. Can you explain what happens?[/red]"

From the description of the catch command: "The catch command may be used to prevent errors from aborting command interpretation".
If you don't catch an error, you can't see any more error because the interpreter is aborted.

"[red]namespace eval ::MyPackage {

# Important Variables
variable my_user(1)
variable my_user(2)
}[/red]"

You misused the variable command.

From the description of the variable command:
"variable ?name value...? name ?value?
...
A name argument cannot reference an element within an array. Instead, name should reference the entire array, and the initialization value should be left off. After the variable has been declared, elements within the array can be set using ordinary set or array commands.
"

Code:
 namespace eval ns \
 {
  variable ar
  array set ar {1 one 2 two}
  set ar(3) three
 }

Some comments:
1-
Code:
variable ar
ensure that var is a variable of the namespace (else it could be an already declared global variable)
2-
Code:
 array set ar ...
ensure that ar is an array

More on variable: More on namespace:
HTH

ulis
 
Beautiful, thank you so much for your answers. Do you know of any good books that cover these types of topics?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top