Overview
●A reintroduction to exceptions
●Lua and exceptions
●A simple try-except construct
●Custom errorobjects
What problem do exceptions solve?
●Reasonable program behavior despite lack of error handling
●Error handling only where needed
●Consistency in raising and handling
●Simpler API’s (Good summary at http://digitalmars.com/d/errors.html)
Exception Concepts
●Raise
●Catch
●Re-raise
●Selective catch
– can apply to any catch scenario
–requires classification of errors
●Exceptions are part ofan API
Usage Scenarios
●Quick scripting
–let everything go unhandled
●Catching errorsfor:
– suppression
– alternate code path
– cleanup (often re-raising)
–retry
–transformation (always re-raising)
●add context
●hide implementation
What should be an error?
●Obvious error:invalid arguments
●Usually not an error:string match failure
● What about file operationfailures (open, delete, rename)?
●Criteria: Ifcaller usually can’tdealwith the situation locally, it’s an error
–i.e. errors usually propagate up two or more stack frames
Lua and exceptions
●Raise witherror(), assert(), lua_error()
●Catch with pcall()
●Implemented withC longjmp()
●Error object not limited to strings
●No try-exceptconstruct
Usage in core and standard library
●Exceptions mainlyused for obvious programming errors
– parse errors
–type errors
–invalid function arguments
●Notable departures: require(), dofile()
●Exclusively string error objects
…
Exception patterns in Lua (211 KO) (Cours PDF)