a={51,31,4,22,23,45,23,43,54,22,11,34}
colors={"white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white","white"}
function try(f, catch_f)
local status, exception = pcall(f)
if not status then
catch_f(exception)
end
end
function refreshColors(yellowEndIndex,redIndex,blueIndex)
for ccnt=1,table.getn(a),1 do
if ccnt < yellowEndIndex then
colors[ccnt] = "yellow"
elseif ccnt == redIndex then
colors[ccnt] = "red"
elseif ccnt == blueIndex then
colors[ccnt] = "blue"
else
colors[ccnt] = "white"
end
end
end
try(refreshColors, function(e)
print("Error Occured - "..e)
end)
refreshColors(1,1,1)
print(colors[1])
当调用 refreshColors() 函数时,它会抛出异常并且错误消息是“发生错误 - trial.lua:11: 尝试将数字与 nil 进行比较”。尽管 refreshColors() 函数中没有这样的比较,为什么会出现异常?