The Lua Minimalist Primer Guide (iii): LoadFile and error handling _lua

Source: Internet
Author: User
Tags assert error handling lua

Compile

Although Lua is an interpretive language, LUA source code is always compiled into an intermediate form and then executed.

Dofile is used to load and execute a LUA file, in contrast, loadfile is used to load a LUA file but does not execute, exactly loadfile compiles a chunk and returns the compiled chunk (as a function):

Copy Code code as follows:

c = LoadFile ('./test.lua ')
C ()

Dofile can be implemented as:

Copy Code code as follows:

function dofile (filename)
Local F = assert (LoadFile (filename))
return F ()
End

A function load similar to LoadFile, which uses a string rather than a file as a parameter to return the compiled chunk:

Copy Code code as follows:

f = Load (' i = i + 1 ')
i = 0
f (); Print (i)--> 1
f (); Print (i)--> 2

In addition, we can use command Luac to compile the Lua files directly:

Copy Code code as follows:

Luac-o PROG.LC Prog.lua
Lua PROG.LC

Error

Lua generates an error when it encounters any unacceptable conditions. For example:

Copy Code code as follows:

Local T = {}
--Error
t = t + 1

We can explicitly call the error function to produce an error, and error accepts an error message as an argument:

Copy Code code as follows:

print ' Enter a number: '
n = io.read (' *n ')
If not n then error (' Invalid input ') end

An assert function can also produce an error. The Assert function checks whether the first argument is false and returns this argument if it is false, and an error occurs. The second argument of assert, the error message, is optional. Example:

Copy Code code as follows:

n = io.read ()
ASSERT (Tonumber (n), ' Invalid input: ' ... n. ' is not a number ')

The Pcall function can catch errors:
Copy Code code as follows:

Local OK, msg = Pcall (function ()
ASSERT (False)
End

Print (OK, msg)

The Pcall function uses protection mode (Protected mode) to invoke the first argument (this parameter is a function), and if the called function does not have an error, Pcall returns True and returns all return values of the called function, and if the called function generates an error, Pcall returns False with the error message attached. Strictly speaking, the error message does not necessarily need to be a string:

Copy Code code as follows:

Local OK, err = Pcall (function ()
Error ({code = 502})
End

Print (Err.code)

Tracking errors

Let's look at a function:

Copy Code code as follows:

function foo (str)
If Type (str) ~= ' string ' then
Error (' string expected ')
End
-- ...
End

Foo (1)

The Foo function requires a string parameter, and we execute the above code:

Copy Code code as follows:

Lua:test.lua:3: String expected

The output indicates that the error occurred in the Foo function (because the Foo function called the error), and in fact, the error was generated by the caller of Foo, not foo. We can set the level to correct this error:

Copy Code code as follows:

function foo (str)
If Type (str) ~= ' string ' then
Error (' String expected ', 2)
End
-- ...
End

The second parameter of the error function is level, which specifies the location of the error, the level value of 1 is the caller of the error, the value 2 represents the caller of the error, and so on.

Pcall can only return error messages, many times we need a complete call stack, at which point the Xpcall function is available. The Xpcall function can receive a message handler as a parameter, and the message handler is invoked to obtain information about the current call stack when an error occurs on the called function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.