Error mechanism

Error handling is critical, because the practice often requires the use of complex operations, including file operations, database transactions and Web services calls.

In any programming, error handling is always needed. Errors can be of two types, which include -

 

Syntax error runtime errors

1. Syntax errors due to incorrect use of the various program components (e.g., operators and expressions) to a syntax error occurs. A simple example of a syntax error is as follows -

a == 2

take

Single "single equal number" and there is a difference between "double equal sign." Improper use may result in an error. A "equal" refers to the assignment, and the two "equal" refers to the comparison.

Another example of a syntax error is as follows -

for a= 1,10

   print(a)

end

take

When the above operation program and obtain following output -

lua: test2.lua:2: 'do' expected near 'print'

Syntax errors than the run-time error handling is easier, because Lua interpreter than a run-time error more clearly positioning error. From the above error, you can easily add do know that statement before the print statement in accordance with Lua structure.

2. Run-time error if a runtime error occurs, the program will succeed, but because of an incorrect or function for error handling can cause a runtime error. Runtime displays shown in the following simple example of error.

function add(a,b)

   return a+b

end

 

add(10)

take

When building the above procedure, it will successfully build and run. After the run, showing runtime error.

lua: test2.lua:2: attempt to perform arithmetic on local 'b' (a nil value)

stack traceback:

    test2.lua:2: in function 'add'

    test2.lua:5: in main chunk

    [C]: ?

This is a run-time error because the variable is not passed two parameters occur. b parameter is expected, but because it is not passed, the default is nil resulting in an error.

Assertions and error function to deal with errors, often use two functions - assert and error. A simple example shown below.

local function add(a,b)

   assert(type(a) == "number", "a is not a number")

   assert(type(b) == "number", "b is not a number")

   return a+b

end

 

add(10)

take

When building the above procedure, it will successfully build and run. After the run, showing runtime error.

lua: test2.lua:3: b is not a number

stack traceback:

    [C]: in function 'assert'

    test2.lua:3: in function 'add'

    test2.lua:6: in main chunk

    [C]: ?

error (message [, level]) protected function terminates the last call, and the message is returned as an error message. This function never returns an error. Typically, the error will add some information about the error position at the beginning of the message. level parameter specifies how to get the wrong location. For 1 (default value) level, error location is wrong to call a function of position. Level 2 points the error function call to call the wrong location and so on. 0 avoid transmission error position information added to the message.

pcall and xpcall in Lua programming in order to avoid these errors and throw error handling, or xpcall pcall required function.

pcall (f, arg1, ...) function call the requested function in the protected mode. If some errors occur in the function f, it will not throw an error. It only returns an error status. Pcall using a simple example is shown below.

function myfunction ()

   n = n / nil

end

 

if pcall(myfunction) then

   print("Success")

else

    print("Failure")

end

take

When building the above procedure, it will successfully build and run. After the run, showing runtime error.

Failure

Function xpcall (f, err) function call requested and an error handler. Any errors will not propagate in f; xpcall capture only the errors, the original error function err object to call, and returns a status code.

A simple example is shown below xpcall function -

function myfunction ()

   n = n / nil

end

 

function myerrorhandler( err )

   print( "ERROR:", err )

end

 

status = xpcall( myfunction, myerrorhandler )

print( status)

take

When running the above procedure, the following will give an output.

ERROR:    test2.lua:2: attempt to perform arithmetic on global 'n' (a nil value)

false

 

Guess you like

Origin www.cnblogs.com/gd-luojialin/p/10962874.html