Lua 异常处理

2019-12-03  本文已影响0人  凌晨还梦

在写lua代码,特别是有热更新功能的项目中,c#中新加了接口之后,然后在lua中调用了此接口。但是项目中c#代码是不能热更的话,而lua代码热更了,旧的代码就会出现以下错误

    field or property xxx does not exist
    stack traceback:
     [C]: in function '__index'

比如 添加 Constants.IsAngle 的调用,使用旧版的C# Constants文件,就会报lua field or property IsAngle does not exist
所以需要添加判断,但是使用 Constants.IsAngle ~= nil 是仍然会报lua field or property IsAngle does not exist错误的,
正确解决方法是添加方法安全套:

if  pcall(function() local x = Constants.IsAngle; end)  then
      
end

lua中的pcall 会将异常吞在里面,而且返回调用是否顺利,有错误会返回false,所以可以通过这种方法来判断c#中是否有这个接口。

上一篇下一篇

猜你喜欢

热点阅读