Catching failed commands and not crashing the program in Tcl
Archive - Originally posted on "The Horse's Mouth" - 2015-10-10 05:07:47 - Graham EllisWhere a command may fail in Tcl (causing Tcl to crash), you can defer it within a catch command. That will return a true value if it fails, and an optional extra parameter gives you the associated error message.
A program to look for the biggest file in or below a directory [here] fails if you run it on a tree with any empty folders, at
set files [glob "$current/*"]
but that problem may be overcome with a catch:
if {[catch {set files [glob "$current/*"]}]} {
set $files ""
}
Updated code [here].
There's a second catch withn our example too - where the code checks for the file size, it's necessary to catch the file size if it might be used on a "special" such as a socket, pipe or device file.
From our Tcl Programming course as run last week.