Simple error-handling instances in Lua _lua

Source: Internet
Author: User

I met another independent game developer yesterday, so I chatted a little more, and then ... And then there was no reading. (Jor: Excuse!) Excuse! )

Let's talk about error handling today, but after all this is just the previous chapter, the contents of the book seems to have a bit of a taste.

It doesn't matter, it's simpler and better ~

1. Red Alert--error

We should be able to frequently see error messages similar to the following:

Copy Code code as follows:

[Lua-print] LUA ERROR: [string "Src/main.lua"]:108: [string "Src/main.lua"]:89:attempt to index Global ' a ' (a nil value)

This is a hint to us in the event of an error, which usually means that our code does not continue to perform properly.

But do you know that we can forge this kind of error, yes, the active call to the error function, this information will appear.
The following code:
Copy Code code as follows:

Error ("Your IQ is not much, hurry to rest, restore it");

Then, run and output the following information:

Copy Code code as follows:

[Lua-print] LUA ERROR: [string ' Src/main.lua ']:109: [string ' Src/main.lua ']:96: You don't have much IQ, so get some rest and get back to it.

This is too powerful, in the future you see who is unhappy, to his code plug this sentence, such as in the project before the line into the ~

To get to the point, when we call a function, we can first determine whether the parameters will be passed is normal, if not normal, we can choose to throw the error directly, easy to write code in the process of discovering problems.

For example, the following code:

Copy Code code as follows:

Local name = Io.read ();
If name ~= "Stupid wood" then
Error ("You are a very kind and caring person, I like you very much.") So, go to hell! ");
End

As long as you find that the input is not "stupid wood", throw the exception mercilessly.

2. Steal a lazy--assert

The code, like the one that just misjudged the error, seems a bit verbose, so we can use assert instead of code like if not then.

For example, the code above should read:

Copy Code code as follows:

Local name = Io.read ();
Local result = ASSERT (name = = "Stupid Wood", "You are a very kind and caring person, I like you very much." So, go to hell! ");

If the first argument of assert is not false, the value of the first parameter is returned; otherwise, the error function is executed, the wrong message is printed, and the error message is the second argument of assert.

The output is the same as before ~

3. Capture error code--pcall

If, in the event of an error, we do not want the code to stop running, but do something urgent, you can use Pcall to catch the error.
such as the following code:

Copy Code code as follows:

function test ()
Print (a[1]);
End
If Pcall (test) Then
Print ("Normal, hehe");
Else
Print ("Ah, the function is wrong, I am helping you deal with it, rest assured, when I wake up ... No, when you wake up will be OK ~ ");

End

The function test executes with a certain error, because there is no table A.
Use Pcall to call the test function, and if test does not error, Pcall returns ture, or false.
With this feature, we can catch exceptions and do some urgent processing.

Run the code and the output is as follows:

Copy Code code as follows:

[Lua-print] Ah, the function is wrong, I am helping you deal with it, rest assured that I wake up on ... No, you'll be fine when you wake up.
The emergency treatment of the way is quite good, hehe. (Jor: Yes, you are!) Does this have anything to do with the treatment? )

In addition to returning true or false, Pcall can return the error message for the function, as follows:
Copy Code code as follows:

function test ()
Print (a[1]);
End
Local status, Err = Pcall (test);
If status Then
Print ("Normal, hehe");
Else
Print ("Ah, the function is wrong, I am helping you deal with it, rest assured, when I wake up ... No, when you wake up will be OK ~ ");
print (ERR);
End

The first return value of the Pcall is the same as before, true or false.

The second parameter, which is the error message thrown by the test function, executes the code, and the result is as follows:

Copy Code code as follows:

[Lua-print] Ah, the function is wrong, I am helping you deal with it, rest assured that I wake up on ... No, you'll be fine when you wake up.
[Lua-print] [string "Src/main.lua"]:94:attempt to index Global ' a ' (a nil value)

4. End

About error handling, there seems to be no fun things, so, write so much ~

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.