lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> Is there a way to introduce exception handling to Lua?

One way is to use errors to raise an exception, and to use "call" to catch 
them. The following code gives the general idea (there are many 
improvements; for instance, you can use the error message to identify the 
exception): 

  -- any error will be catched by the "call" (see below)
  function foo (a1, a2, ...)
    ...
    foo2(...)
    ...
  end
  
  function foo2 (...)
     ...
     if bla-bla-bla then error() end   -- raises an "exception"
  end
 
 
  -- "try" foo
  if not call(foo, {arg1, arg2, ...}, "x", nil) then
    -- here goes the "catch" code
    ...
  end


-- Roberto