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