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!

diagnose open failures

Status
Not open for further replies.

judyj

Programmer
Oct 15, 2002
4
US
An open call is returning an ivalid fileid on the following line:


set fileid [open $fileName r]

if I try set catchval [catch {set fileid [open fieleName r]}]

catchval will be set to 1. Where can I find the definitions of the error codes?
 
Hi Judy,
No you should do it the way I previously posted.
Catch will return the tcl specific error
in the catch defined variable.

if {[catch {set fd [open $file r]} err_open]} {
puts "$err_open occurred."
}

catch returns 0 on success and 1 on failure.
 
In fact catch can get a little more but 0 and 1 are the most significant ones.

Catch can get:
0 TCL_OK normal return
1 TCL_ERROR error return
2 TCL_RETURN cause the *caller* to return
3 TCL_BREAK call [break] in the caller
4 TCL_CONTINUE call [continue] in the caller

And more: all codes returned with [return -code ...].

From the return entry of Tcl manual:
EXCEPTIONAL RETURNS

In the usual case where the -code option isn't specified the procedure will return normally (its completion code will be TCL_OK). However, the -code option may be used to generate an exceptional return from the procedure. Code may have any of the following values:

ok
Normal return: same as if the option is omitted.
error
Error return: same as if the error command were used to terminate the procedure, except for handling of errorInfo and errorCode variables (see below).
return
The current procedure will return with a completion code of TCL_RETURN, so that the procedure that invoked it will return also.
break
The current procedure will return with a completion code of TCL_BREAK, which will terminate the innermost nested loop in the code that invoked the current procedure.

continue
The current procedure will return with a completion code of TCL_CONTINUE, which will terminate the current iteration of the innermost nested loop in the code that invoked the current procedure.
value
Value must be an integer; it will be returned as the completion code for the current procedure.

HTH

ulis
 
I'm assuming that it's only a transcription error that you omitted the "$" in front of "fieleName" in the example where you tried to use the catch command. But I thought I'd point it out, just in case.

Anyway, as has been mentioned previously, all built-in Tcl commands that generate an error condition do so with a return code of "1" (which is the value that the catch command would return in this case). Successful execution is indicated by a return code of "0". In practice, you don't have to worry about other possible return codes unless you're creating your own control structures in Tcl. But the upshot is that the return code isn't going to help you much in debugging the error.

The general syntax of catch is:

[tt]catch action varName[/tt]

If the action executes without any error, the return value of the last command executed in the action is stored in the variable given by varName. In the case of an error, the error message is stored in varName.

So, when catch indicates an error, checking the value of varName will tell you what error occurred. The code fragment that marsd gave above should print out the error message if an error occurs when opening the file. Actually, his example could be simplified somewhat as:

Code:
if {[catch {open $file r} fd]} {
    puts "$fd occurred."
}

What leads you to believe that open is returning an invalid file identifier? Personally, I can't think of any case in which Tcl's built-in open command would ever return an "invalid" file identifier. open can raise error conditions if it can't successfully open a file, but in that case, no file identifier is returned. Some situations that I can think of that might cause open to generate an error are:[ul][li]The file specified doesn't exist[/li][li]Your script doesn't have permission to read the file (or write to it, depending on the mode in which you're opening it)[/li][li]The file is in some way locked by another application[/li][/ul]If you could post the error message returned by open, perhaps someone here could figure out what's going wrong. - Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top