Error Handling - Canon Camera Hackers Manual

Camera hackers manual berthold daum
Table of Contents

Advertisement

100
C H A P T E R 5
Scripting
hello =
function() print('hello') end
The function can later be invoked via
hello()

5.4.10 Error handling

Another big advantage of Lua over uBasic is the ability to run pieces of code
in protected mode. This means that an error condition detected inside this
code is not propagated to the surrounding code. Your script can gracefully
handle this error and continue to run. The built-in function pcall() can
execute other functions in protected mode. For example:
status,result = pcall(stopwatch,k)
As you can see, pcall() can return multiple values:
If no error occurred, the first return value (status) has the value true,
f
followed by the return values (if any) of the called function. In the above
example, result would contain the result of function stopwatch().
If an error occurred, the first returned value is false, followed by the
f
error message in the second return value.
In addition to the errors raised by Lua itself, errors can also raised by a script
through the error() function. The first parameter of this function is an
error message. The optional second parameter may contain an error level
that indicates which additional information the Lua interpreter will add to
the error message:
0: no additional information.
f
1 (default): the location where the error() function was called.
f
n: the error location n levels up in the caller hierarchy.
f
Lua also knows an assert instruction, which is usually used to test the
conditions under which a piece of code is allowed to run.
assert(cond, msg)
If the expression cond results in false, an error is raised with the content of
the parameter msg as an error message. The code following the assert

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Powershot sx10 is

Table of Contents