快速体验,学习lua(一种可嵌入c++,c#,android,object-c等并进行互调支持热更新的脚本)的入门调试系列(3)

--这里是注释
--[[
功能备注:lua快速体验,学习,了解语法(调试,类似try-catch)
创建时间:2020-6-27
创建人:pcw
--]]
print("--------------------------------");
print("类似throw exception(该方法抛异常之后,整个代码文件不往下走.)");
 function addTestThrowException(a)
   assert(type(a) == "number", "a 不是一个数字")  --异常之后,后面的代码不执行
   return a*a;
end
print(addTestThrowException(5));
--[[
  输出结果:
  类似throw exception
25
]]
--print(addTestThrowException("ab"));
--[[
解开上面的注释,输出结果:
lua: Lua-调试.lua:10: a 不是一个数字
stack traceback:
    [C]: in function 'assert'
    Lua-调试.lua:10: in function 'addTestThrowException'
    Lua-调试.lua:19: in main chunk
    [C]: ?
]]

print("--------------------------------");
print("pcall:类似try-catch但不提供  调试信息");
print("pcall(addTestThrowException,5)=",pcall(addTestThrowException,5));
print("pcall(addTestThrowException,\"a\")=",pcall(addTestThrowException,"a"));



print("--------------------------------");
print("xpcall类似try-catch但且提供调试信息  ");
local function addTestThrowException2(a)
   print(a);
   return a*a;
end

function processException(err)
   --debug.debug();
   --print(debug.debug());
   --print("ERROR:",err);
   --print(debug.getinfo(1))
   debug.traceback();
   return false;
end
print(addTestThrowException2(5));
xpcall(addTestThrowException2,processException,5);

猜你喜欢

转载自www.cnblogs.com/taohuadaozhu/p/13198157.html