| |||||||||||
| |||||||||||
Error handling in Tcl through catch
Tcl is a truely interpretive language - and that means that you can get a syntax error if a user makes an illegal entry ... even in the middle of a program running. For example
Will run correctly if you enter a number to the prompt as requested: but will fail if you enter something that isn't a number: If you want to make your Tcl code rugged against such errors, embed commands that may cause a problem within catch commands. Tcl's catch command returns a true of false result depending on whether or not the embedded command succeeds, and if it fails it also returns the error message decribing the problem in a variable of your choice. Here's the program above, enhanced to catch any errors: And let's try running that. Firstly, with correct data: And now with data that would previously have caused a crash: This approach described here could be described as an expection handling system as it tries to perform an operation and traps if a failure occurs, rather than trying to predict the error ahead of time. An alternative (error handling) system could have been used, in which the code check the input text string to make sure that it's numeric before the expr command is run. We have a comparison of exceptions v errors in our solution centre. (written 2005-07-02 09:23:42) Associated topics are indexed under T214 - Tcl/Tk - Other Facilities in Tcl
Some other Articles
From IcelandCMS - the minefield of Choices Vacation Week Ajax Error handling in Tcl through catch Letterbox Pictures pu daily and p hourly Greetings from Edinburgh The ireallyreallywanna operator Binary Large Objects or bars 1635 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 at 50 posts per pageThis 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). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||