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:
f
If no error occurred, the first return value (
status
) has the value
true
,
followed by the return values (if any) of the called function. In the above
example,
result
would contain the result of function
stopwatch()
.
f
If an error occurred, the first returned value is
false
, followed by the
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:
f
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.
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
Summary of Contents for Camera
Page 1: ......
Page 2: ...The Canon Camera Hackers Manual ...
Page 3: ......
Page 4: ...Berthold Daum The Canon Camera Hackers Manual Teach Your Camera New Tricks ...
Page 19: ...10 CH APTER 2 Cameras and Operating Systems ...
Page 25: ...16 CH APTER 3 ...
Page 85: ...76 CH APTER 4 Teach Your Camera New Tricks ...
Page 213: ...204 CH APTER 6 ...
Page 253: ...244 AP PENDIX ...