Reputation: 29
dofile("x/y/m.lua")
dofile("x/y/p.lua")
if m.lua fails due to some issue , p.lua will not run at all, please give me some resolution that i can run both files even if the first one fails and have logs for both
Upvotes: 0
Views: 894
Reputation: 462
Use pcall to catch Lua errors, possibly like this:
local success, result = pcall(dofile, "foo.lua")
If success
is false, the function failed and the error message will be in result
. If success
is true, the return values of dofile
will be in result
. You can add additional result
variables. For example:
local success, result1, result2, result3 = pcall(dofile, "foo.lua")
Upvotes: 0
Reputation: 72312
Try
function dofile(name)
local f,err=loadfile(name)
if f==nil then print(err) end
local ok,err=pcall(f)
if not ok then print(err) end
end
Upvotes: 1