For 2021 - online Python 3 training - see ((here)).
Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7. We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.
Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc. |
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 ("Nah!")
else
print ("That is half of ",value * 2)
end
( 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 articles U111 - Lua - Trapping and handling errors. [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15) [2706] Error trapping in Lua - no exceptions. - (2010-04-04) G996 - Well House Consultants - Newsletter Scripts [1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19) [1066] Final, Finally and Finalize - three special words in Java - (2007-02-05) [1123] mysqldump and mysqlrestore - (2007-03-30) [1217] What are factory and singleton classes? - (2007-06-04) [1321] Resetting session based tests in PHP - (2007-08-26) [1387] Error logging to file not browser in PHP - (2007-10-11) [1487] Efficient PHP applications - framework and example - (2007-12-28) [1505] Script to present commonly used images - PHP - (2008-01-13) [1601] Replacing the last comma with an and - (2008-04-04) [1665] Factory method example - Perl - (2008-06-04) [1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07) [1813] Ajax - going Asyncronous and what it means - (2008-09-28) [1954] mod_rewrite for newcomers - (2008-12-20) [2046] Finding variations on a surname - (2009-02-17) [2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29) [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27) [2433] Controlling, supressing, enabling PHP error messages - (2009-10-02) [2539] Changing Images - (2009-12-17) [3179] Oops - I typed ci not vi, and have lost my file ... - (2011-02-21)
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
|
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).
|
|