What could possibly go wrong when you open a file in a program? Lots of things, actually.
* You could try to open a file in directory you cannot access
* You could try to read from a file that does not exist
* You could try to read from something that's not a plain file - e.g. a directory
* You could be trying to read from a file over which you don't have read access
And I'm sure this list is incomplete ...
And if you're writing a program that should be robust, you need to consider all of the above - including that comment
"this list is incomplete".
Tests in your program to check that things will work are often a good idea. But they will only test the circumstances that you have come up with - in other words, they are not "fail safe". In some languages, you can check the return status from your file open command and see if it worked - that's a better solution but, alas, in some circumstances / languages such as Tcl your open command won't even return to you to tidy up if it fails, and you need something else. And that's where you need to use
exceptions - keywords like
try are usually used.
In Tcl - which I have been teaching this week - there's a
catch command, and this allows you to wrap a command that may fail, getting back a true value if that failure occurs, and (optionally) a variable containing the error message so that you can report it if you wish. Here's an example:
if {[catch {set fh [open $day r]} excuse]} {
puts "Failed to open - $excuse"
continue }
Full example
[here] from our
Learning to program in Tcl course.
(written 2012-01-06, updated 2012-01-07)
Associated topics are indexed under
T214 - Tcl/Tk - Other Facilities in Tcl [3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
[3287] Exceptions - Tcl style - (2011-05-12)
[2467] Tcl - catching an error before your program crashes - (2009-10-22)
[1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[461] Shortened interactive commands - (2005-10-11)
[366] Error handling in Tcl through catch - (2005-07-02)
[364] pu daily and p hourly - (2005-06-30)
[239] What and why for the epoch - (2005-03-08)
T203 - Tcl/Tk - Conditionals and Loops [3571] Comparing loop commands in Tcl - (2012-01-06)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3189] Tcl - the danger of square brackets in a while command - (2011-03-02)
[2681] Tcl - a great engineering language - (2010-03-17)
[2471] A short form of if ... then ... else - (2009-10-23)
[2261] Tcl - nice and nasty - (2009-06-29)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1477] Decisions - small ones, or big ones? - (2007-12-18)
[1401] Tcl - using [] or {} for conditions in an if (and while) - (2007-10-23)
[210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
Some other Articles
Perl functions such as chop change their input parametersNew in Java 7 - and why we are not running public Java 7 coursesAdding Expect on top of Tcl - what is it and where can I get a training course to learn about it?Trapping errors in Tcl - the safety net that catch providesImages of the new year in MelkshamTelling which ServerAlias your visitor used - useful during merging domainsFirst of the yearInitial thoughts - response to GW Rail Franchise Consultation for WiltshireWhat would an appropriate train service on the TransWilts line mean