
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 under
G996 - Well House Consultants - Newsletter Scripts [3179] Oops - I typed ci not vi, and have lost my file ... - (2011-02-21)
[2539] Changing Images - (2009-12-17)
[2433] Controlling, supressing, enabling PHP error messages - (2009-10-02)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
[2046] Finding variations on a surname - (2009-02-17)
[1954] mod_rewrite for newcomers - (2008-12-20)
[1813] Ajax - going Asyncronous and what it means - (2008-09-28)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1665] Factory method example - Perl - (2008-06-04)
[1601] Replacing the last comma with an and - (2008-04-04)
[1505] Script to present commonly used images - PHP - (2008-01-13)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1387] Error logging to file not browser in PHP - (2007-10-11)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1217] What are factory and singleton classes? - (2007-06-04)
[1123] mysqldump and mysqlrestore - (2007-03-30)
[1066] Final, Finally and Finalize - three special words in Java - (2007-02-05)
[1001] .pdf files - upload via PHP, store in MySQL, retrieve - (2006-12-19)
U111 - Lua - Trapping and handling errors. [2706] Error trapping in Lua - no exceptions. - (2010-04-04)
[2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15)
5178
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