Lua analyzes the error handling mechanism when running in C Language

Source: Internet
Author: User

When calling the Lua Script Engine in C language, you must identify and handle script errors.

1. Error Type

There are five Lua errors, corresponding to five macro definitions:

# Define lua_yield1 // thread suspended <br/> # define lua_errrun2 // runtime error <br/> # define lua_errsyntax3 // compilation error <br/> # define lua_errmem4 // memory Allocation Error <br/> # define lua_errerr5 // error that occurs when the error processing function is running

Generally, there are two types of errors: runtime errors and compilation errors.

2. Error Return Method

Whether it is a runtime error or a compilation error, the error information is returned to the top layer of the stack. You can print the specific error information using the following code:

Const char * error = lua_tostring (L,-1); // print the error result <br/> printf ("% s", error); <br/> lua_pop (L, 1 );

3. Which functions need to be added with the error analysis mechanism?

The Lua script engine can be used in two steps (the initialization process is not included here)

Step 1 is Script Loading: some of the following functions

Int lual_loadbuffer (lua_state * l, <br/> const char * buff, <br/> size_t SZ, <br/> const char * Name ); <br/> int lual_loadfile (lua_state * l, const char * filename); <br/> int lual_loadstring (lua_state * l, const char * s ); <br/>/* In fact, these functions are for int lua_load (lua_state * l, <br/> lua_reader reader, <br/> void * data, <br/> const char * chunkname); encapsulation <br/> */

All these functions generate compile-time errors. That is, only compile-time errors can be returned through the return values of these functions.

Step 2 is script execution, and some functions are as follows:

Lua_pcall (lua_state * l, int nargs, int nresults, int errfunc );

This function returns a runtime error.

Like some other functions below, the process of combining functions in step 1 and Step 2 has not changed. These functions are roughly as follows:

Int lual_dostring (lua_state * l, const char * Str); <br/> int lual_dofile (lua_state * l, const char * filename );

You can view the help documentation

4. Error Handling example

The following code processes error handling information and prints error information:

# Include "stdafx. H "<br/> extern" C "{<br/> # include" Lua. H "<br/> # include" lualib. H "<br/> # include" lauxlib. H "<br/>}< br/> void printluaerror (lua_state * l, int serr) <br/>{< br/> If (serr = 0) <br/>{< br/> return; <br/>}< br/> const char * error; <br/> char serrortype [256] = {0 }; <br/> switch (serr) <br/> {<br/> case lua_errsyntax: // compilation error <br/>/* const char * Buf = "mylib. myfun () 2222 "; statements similar to this line can cause compilation errors */<br/> spri Ntf_s (serrortype, sizeof (serrortype), "% s", "syntax error during pre-compilation"); <br/> break; <br/> case lua_errmem: // memory error <br/> sprintf_s (serrortype, sizeof (serrortype), "% s", "memory allocation error"); <br/> break; <br/> case lua_errrun: // runtime error <br/>/* const char * Buf = "my222lib. myfun () "; a statement similar to this line can cause a runtime error. my222lib does not actually have such a library, and the returned value is nil */<br/> sprintf_s (serrortype, sizeof (serrortype ), "% s", "a runtime error"); <Br/> break; <br/> case lua_yield: // thread suspension error <br/> sprintf_s (serrortype, sizeof (serrortype), "% s ", "thread has suincluded"); <br/> break; <br/> case lua_errerr: // an error occurred during error handling <br/> sprintf_s (serrortype, sizeof (serrortype), "% s", "error while running the error handler function"); <br/> break; <br/> default: <br/> break; <br/>}< br/> error = lua_tostring (L,-1); // print the error result <br/> printf ("% s: % s", serrortype, error); <br/> Lua _ Pop (L, 1); <br/>}< br/> int main () <br/>{< br/> lua_state * l = lual_newstate (); <br/> lual_openlibs (l); <br/> const char * Buf = "mylib. myfun () "; // note the call rules <br/> int S = lual_loadstring (L, Buf ); // parse the code and do not execute <br/> const char * error; <br/> If (S = 0) <br/>{< br/> S = lua_pcall (L, 0, lua_multret, 0); <br/> If (s! = 0) <br/>{< br/> printluaerror (L, S ); <br/>}< br/> else <br/> {<br/> printluaerror (L, S ); <br/>}< br/> lua_close (l); <br/> getchar (); <br/> return 0; <br/>}< br/>

7. Error clearing

You can run the int lua_error (lua_state * l); statement to execute a jump command. By default, exit the program, that is, exit (exit_failure );

You can also define the jump function by yourself. This part has not been studied in depth yet.

 

 

 

 

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.