RAGHAV123
RAGHAV123

Reputation: 29

How to run multiple lua scripts through a single lua script even if one script fails

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

Answers (2)

ECrownofFire
ECrownofFire

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

lhf
lhf

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

Related Questions