Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Trapping errors in Tcl - the safety net that catch provides

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 as below, or enter http://melksh.am/nnnn for individual articles
T214 - Tcl/Tk - Other Facilities in Tcl
  [239] What and why for the epoch - (2005-03-08)
  [364] pu daily and p hourly - (2005-06-30)
  [366] Error handling in Tcl through catch - (2005-07-02)
  [461] Shortened interactive commands - (2005-10-11)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [782] Converting between Hex and Decimal in Tcl - (2006-06-28)
  [1277] AgtInvoke - a command to drive Agilent Tcl software extensions - (2007-07-26)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
  [2467] Tcl - catching an error before your program crashes - (2009-10-22)
  [3287] Exceptions - Tcl style - (2011-05-12)
  [3583] Expanding a list of parameters in Tcl - {*} and eval - (2012-01-17)
  [4207] Exception handling in Tcl - (2013-11-14)
  [4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
  [4525] What does Tcl do if you try to run a command that is not defined? - (2015-10-10)
  [4762] Coverage map in Tcl - how many times has each proc been called? - (2017-09-28)

T203 - Tcl/Tk - Conditionals and Loops
  [210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
  [1401] Tcl - using [] or {} for conditions in an if (and while) - (2007-10-23)
  [1477] Decisions - small ones, or big ones? - (2007-12-18)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [2261] Tcl - nice and nasty - (2009-06-29)
  [2471] A short form of if ... then ... else - (2009-10-23)
  [2681] Tcl - a great engineering language - (2010-03-17)
  [3189] Tcl - the danger of square brackets in a while command - (2011-03-02)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [3571] Comparing loop commands in Tcl - (2012-01-06)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4455] Working out distance between places, using OS grid references and a program in Tcl - (2015-03-11)


Back to
Images of the new year in Melksham
Previous and next
or
Horse's mouth home
Forward to
Comparing loop commands in Tcl
Some other Articles
Perl functions such as chop change their input parameters
New in Java 7 - and why we are not running public Java 7 courses
Adding 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 provides
Images of the new year in Melksham
Telling which ServerAlias your visitor used - useful during merging domains
First of the year
Initial thoughts - response to GW Rail Franchise Consultation for Wiltshire
What would an appropriate train service on the TransWilts line mean
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/3570_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb