1hunch1kill
1hunch1kill

Reputation: 520

Error Handling In Lua

I would like to do logic like:

local function create(...)
    for k, v in ipairs{...} do
       if k == "player" then
           _player = v
       end
    end
    if _player == nil then
         **error**("It nil") -- stop running here and throw the error
    end
end

Does Lua has anything like error function here?

Upvotes: 1

Views: 611

Answers (1)

Yu Hao
Yu Hao

Reputation: 122493

Yes, there is, and its name is exactly error():

if _player == nil then
     error("It nil") -- stop running here and throw the error
end

error() takes a string argument for error message and an optional argument for level, it terminates the program when called.

Upvotes: 3

Related Questions