Home Accessibility Courses Diary The Mouth Facebook Resources Site Map About Us Contact
Error Handling in Lua with assert and pcall

On site course - students learning LuaWhen 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
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)


Back to
A fresh example - objects the Lua way
Previous and next
or
Horse's mouth home
Forward to
Geocoding - converting address to latitude / Longitude with PHP via Google
Some other Articles
Getting it right from the start - new programmers
Alpaca Case or Camel Case
Thornham Farm Shop, Seend Cleeve, nr Melksham
Geocoding - converting address to latitude / Longitude with PHP via Google
Error Handling in Lua with assert and pcall
A fresh example - objects the Lua way
Encapsulating shared code in a Lua module
Checking if the user has entered a number (Lua)
Passing multiple results back from a function - Lua, Python, etc
See this page in French
4252 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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2360_Err ... pcall.html • PAGE BUILT: Sun Mar 30 15:20:58 2014 • BUILD SYSTEM: WomanWithCat