Lua错误处理如何优化?

更新于
2026-07-26 11:31:08
20阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计211个文字,预计阅读时间需要1分钟。

Lua错误处理如何优化?

我尝试了使用lua的文件系统库,但无法访问某些目录并引发了错误。这可能是由于luaL_error导致的。具体信息可以参考:https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c

我是lua的新手.

我试过用
keplerproject.github.io/luafilesystem/examples.html
并且它在无法访问的目录上引发错误.

这似乎是由luaL_error github.com/keplerproject/luafilesystem/blob/master/src/lfs.c#L563引起的

我怎么能抓到这个错误?
www.tutorialspoint.com/lua/lua_error_handling.htm
建议pcall,但这不会阻止脚本死亡:

pcall(lfs.dir('/etc/passwd')) #this fails to handle the not a directory error pcall(lfs.dir(‘/ etc / passwd’))失败,因为错误是在pcall之外触发的(当计算pcall的参数时).你需要使用

local ok, res = pcall(lfs.dir, '/etc/passwd')

请注意,传递给lfs.dir的参数是给pcall的,而不是lfs.dir.

Lua错误处理如何优化?

本文共计211个文字,预计阅读时间需要1分钟。

Lua错误处理如何优化?

我尝试了使用lua的文件系统库,但无法访问某些目录并引发了错误。这可能是由于luaL_error导致的。具体信息可以参考:https://github.com/keplerproject/luafilesystem/blob/master/src/lfs.c

我是lua的新手.

我试过用
keplerproject.github.io/luafilesystem/examples.html
并且它在无法访问的目录上引发错误.

这似乎是由luaL_error github.com/keplerproject/luafilesystem/blob/master/src/lfs.c#L563引起的

我怎么能抓到这个错误?
www.tutorialspoint.com/lua/lua_error_handling.htm
建议pcall,但这不会阻止脚本死亡:

pcall(lfs.dir('/etc/passwd')) #this fails to handle the not a directory error pcall(lfs.dir(‘/ etc / passwd’))失败,因为错误是在pcall之外触发的(当计算pcall的参数时).你需要使用

local ok, res = pcall(lfs.dir, '/etc/passwd')

请注意,传递给lfs.dir的参数是给pcall的,而不是lfs.dir.

Lua错误处理如何优化?