This week, we're updating our course layouts and descriptions. Presentation and materials always gently change over time, but just occasionally there's a need to make a step change to clear out some of the old and roll in the new. That's now happening - but over a long and complex site it's not instant and you'll see sections of the site changing up to and including 19th September.
See also [here] for status update
Error Handling in Lua with assert and pcall
When a function fails in lua
, you'll often find that it return a nil
value that you can check, or it returns two values, the second of which is an error status / message that tells you what went wrong.
Here's an example:
value = tonumber(stuff)
if value == nil then
print ("That is half of ",value * 2)
(full source of example
You can reduce the code somewhat by using the built in assert function to check, generate the message, and exit with a full stack trace - example:
io.write ("How many bits: ")
val2 = tonumber(io.read())
assert(val2 >= 1,"need to have at least one piece")
(full source code of example
There are times when a lua function will generate an error before it returns to the calling code .. but you want to trap that
.. and you can do so via pcall
or protected call. Example:
goodun,eachlen = pcall(cutter,val1,val2)
This calls the cutter
function with two parameters, and the result is returned into eachlen
. The other return variable is a status to tell you whether or not the function worked.
(full source code of example
Illustration - during a private Lua course at a customer's office. As well as private training courses on site, we run public Lua courses at our Melksham, Wiltshire, UK training centre. See [here]. (written 2009-08-13, updated 2010-06-19)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesG996 - Well House Consultants - Newsletter Scripts 
Oops - I typed ci not vi, and have lost my file ... - (2011-02-21) 
Changing Images - (2009-12-17) 
Controlling, supressing, enabling PHP error messages - (2009-10-02) 
Grouping rows for a summary report - MySQL and PHP - (2009-06-27) 
Using the internet to remotely check for power failure at home (PHP) - (2009-04-29) 
Finding variations on a surname - (2009-02-17) 
mod_rewrite for newcomers - (2008-12-20) 
Ajax - going Asyncronous and what it means - (2008-09-28) 
First class functions in Lua lead to powerful OO facilities - (2008-08-07) 
Factory method example - Perl - (2008-06-04) 
Replacing the last comma with an and - (2008-04-04) 
Script to present commonly used images - PHP - (2008-01-13) 
Efficient PHP applications - framework and example - (2007-12-28) 
Error logging to file not browser in PHP - (2007-10-11) 
Resetting session based tests in PHP - (2007-08-26) 
What are factory and singleton classes? - (2007-06-04) 
mysqldump and mysqlrestore - (2007-03-30) 
Final, Finally and Finalize - three special words in Java - (2007-02-05) 
.pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)U111 - Lua - Trapping and handling errors. 
Error trapping in Lua - no exceptions. - (2010-04-04) 
Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
Some other Articles
Getting it right from the start - new programmersAlpaca Case or Camel CaseThornham Farm Shop, Seend Cleeve, nr MelkshamGeocoding - converting address to latitude / Longitude with PHP via GoogleError Handling in Lua with assert and pcallA fresh example - objects the Lua wayEncapsulating shared code in a Lua moduleChecking if the user has entered a number (Lua)Passing multiple results back from a function - Lua, Python, etcSee this page in French
4284 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 at 50 posts per page
This is a page archived from The Horse's Mouth at
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).