I am currently using this statement to load tables from disk. I find the error message from load* deplorable.
for my first test, I am using a table that does load with this statement, and it is trivial to make a table saved to a file that will not load, that is next.
_otbl, _err = assert(loadfile(_pth)())
I am trying to get pcall to work, or xpcall and have never gotten them to work without help, I simply do not understand the documentation, and useful examples are non-extant.
if I include the exec () I get true for the _otbl:
_otbl, _err = pcall(function() loadfile(_pth)() end)
if _err then
error(('File %s is corrupt. Cannot continue.'):format(_pth))
end
_otbl, _err = xpcall(function() loadfile(_pth)() end,
function(_err) -- err is the error message
return (error(('File %s is corrupt. Cannot continue.'):format(_pth)))
end)
if I do not include the exec () I get an empty _otbl, so it is not 'exec-ing':
_otbl, _err = pcall(function() loadfile(_pth) end)
if _err then
error(('File %s is corrupt. Cannot continue.'):format(_pth))
end
_otbl, _err = xpcall(function() loadfile(_pth) end,
function(_err) -- err is the error message
return (error(('File %s is corrupt. Cannot continue.'):format(_pth)))
end)
I must be missing something very fundamental here, I am just trying to make a better error statement.
* assert, pcall, xpcall
assert, pcall, xpcall
FH V.6.2.7 Win 10 64 bit
- tatewise
- Megastar
- Posts: 28341
- Joined: 25 May 2010 11:00
- Family Historian: V7
- Location: Torbay, Devon, UK
- Contact:
Re: assert, pcall, xpcall
There are several syntax errors in what you have posted, so they need fixing first.
- loadfile(_pth)() is an odd construction with trailing () so should be local _otbl, _err = loadfile(_pth)
- _otbl, _err = assert(loadfile(_pth)()) will fail for the same reason so try local _otbl, _err = assert(loadfile(_pth))
- _otbl, _err = pcall(function() loadfile(_pth)() end) is the wrong syntax.
The first parameter f is the name of the function to call and the rest are its parameters.
So try local _pc, _otbl, _err = pcall(loadfile,_pth)
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Re: assert, pcall, xpcall
this is what gets the job done, I now can know where tables on file goes wrong, and works well if tables are right.
now to revisit filesystemobject instead of rename.
_, _otbl, _err = pcall(loadfile, _pth)
if _err then
_err = _err:gsub(('%s:'):format(_pth or ''), '')
error(('File %s is corrupt. %s'):format(_pth, _err))
end
_otbl = _otbl()
now to revisit filesystemobject instead of rename.
_, _otbl, _err = pcall(loadfile, _pth)
if _err then
_err = _err:gsub(('%s:'):format(_pth or ''), '')
error(('File %s is corrupt. %s'):format(_pth, _err))
end
_otbl = _otbl()
FH V.6.2.7 Win 10 64 bit