2014-04-18 46 views
1

我想這樣做的邏輯,如:錯誤處理Lua中

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 

不lua有類似的東西在這裏誤差函數?

+1

在這段代碼中'k'永遠不會是字符串'player'。 「ipairs」返回的第一個值是數字索引。 –

+0

@Etan Reisner,明白了,我可以在這裏使用pairs()...嗎?否則有沒有辦法傳遞字符串鍵? – 1hunch1kill

+0

你打算如何調用'create'函數? 'create(「player」,arg1,arg2,arg2)'? 'create({player =「something」,attr1 = val1,attr2 = val2})? –

回答

3

是的,有,它的名字也正是error()

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

error()花費的錯誤信息和水平的可選參數字符串參數,調用時就終止程序。

+0

除非調用者使用pcall或xpcall來調用調用錯誤的函數,否則調用者可以處理錯誤,不會退出。 – Schollii