c#與lua交互裏,錯誤處理

若是是c#代碼出錯了c#

[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static int _g_get_down(RealStatePtr L)
{
try {
ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);
translator.PushUnityEngineVector3(L, UnityEngine.Vector3.down);
} catch(System.Exception gen_e) {
return LuaAPI.luaL_error(L, "c# exception:" + gen_e);
}
return 1;
}

  會在全部導出的方法裏,增長try  catch,而後把錯再拋到lua裏。app

若是是lua代碼出錯了lua

[MonoPInvokeCallback(typeof(LuaCSFunction))]
        internal static int Panic(RealStatePtr L)
        {
            string reason = String.Format("unprotected error in call to Lua API ({0})", LuaAPI.lua_tostring(L, -1));
            throw new LuaException(reason);
        }

  

       public void ThrowExceptionFromError(int oldTop)
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnvLock)
            {
#endif
                object err = translator.GetObject(L, -1);
                LuaAPI.lua_settop(L, oldTop);

                // A pre-wrapped exception - just rethrow it (stack trace of InnerException will be preserved)
                Exception ex = err as Exception;
                if (ex != null) throw ex;

                // A non-wrapped Lua error (best interpreted as a string) - wrap it and throw it
                if (err == null) err = "Unknown Lua Error";
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
				    toLuaError toLua = LuaVM.Instance.luaEnv.Global.GetInPath<toLuaError>("reportError");
				    toLua(err.ToString());
#endif
            throw new LuaException(err.ToString());
#if THREAD_SAFE || HOTFIX_ENABLE
            }
#endif
        }

  會同時拋出c#的exception,這樣c#的棧才能正確處理orm

相關文章
相關標籤/搜索