-- 打印错误信息
local function __TRACKBACK__(errmsg)
local track_text = debug.traceback(tostring(errmsg), );
print("---------------------------------------- TRACKBACK ----------------------------------------");
print(track_text, "LUA ERROR");
print("---------------------------------------- TRACKBACK ----------------------------------------");
local exception_text = "LUA EXCEPTION\n" .. track_text;
return false;
end --[[ 尝试调一个function 这个function可以带可变参数
如果被调用的函数有异常 返回false,退出此方法继续执行其他代码并打印出异常信息;]]
function trycall(func, ...)
local args = { ... };
return xpcall(function() func(unpack(args)) end, __TRACKBACK__);
end
--测试代码: trycall(function(param)
      print("message "..param)
      print("message "..nil)
end, "test trycall")

##输出结果如下:

>lua -e "io.stdout:setvbuf 'no'" "itertor_test.lua"
message test trycall
---------------------------------------- TRACKBACK ----------------------------------------
itertor_test.lua:45: attempt to concatenate a nil value
stack traceback:
itertor_test.lua:43: in main chunk
[C]: ? LUA ERROR
---------------------------------------- TRACKBACK ----------------------------------------
>Exit code: 0

xpcall (f, err)

This function is similar to pcall,except that you can set a new error handler.

xpcall calls function f in protected mode,using err as the error handler.Any error inside f is not propagated;instead, xpcall catches the error,calls the err function with the original error object,and returns a status code.Its first result is the status code (a boolean),which is true if the call succeeds without errors.In this case, xpcall also returns all results from the call,after this first result.In case of any error,xpcall returns false plus the result from err.

lua以xpcall实现try/catch功能的更多相关文章

  1. Cocos2d-x教程(26)-Cocos2d-x + Lua脚本实现大地图缩放功能

    欢迎增加 Cocos2d-x 交流群: 193411763 视频教程地址:http://www.tudou.com/programs/view/qRiOfppMghM/ 转载请注明原文出处:http: ...

  2. 触摸精灵lua脚本实现微信群加好友功能

    --此脚本针对iPhone5/5s --目前只实现一个屏幕微信群好友的添加,如果想添加更多好友, --需要划屏操作,执行划屏操作时建议循环滑动 function main() --获取屏幕的分辨率 w ...

  3. Lua 架构 The Lua Architecture

    转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间 ...

  4. Lua脚本在redis分布式锁场景的运用

    目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...

  5. Lua中的loadfile,dofile,require使用,最后还有调试

    1.loadfile---只编译,不运行. loadfile编译代码成中间码并且返回编译后的chunk作为一个函数,而不执行代码:另外loadfile不会抛出错误信息而是返回错误代号. loadstr ...

  6. lua工具库penlight--01简介

    lua的设计目标是嵌入式语言,所以和其它动态语言(如python.ruby)相比其自带的库缺少很多实用功能. 好在有lua社区有Penlight,为lua提供了许多强大的功能,接下来的几篇博客,我会简 ...

  7. Lua语言中文手册 转载自网络

    Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...

  8. Sublime Text(2/3)编译lua

    想在subLime text 3中集成编译Lua(或其他语言)功能,只需要在Tool->Build System -> New Build System中将原来的 { "shel ...

  9. 出售一套Unity + Lua热更新框架代码

    出售一套Unity + Lua的客户端框架代码,功能有资源管理.网络通信.配置文件解析.热更新.文件读写.Lua加密揭秘.UI框架.打包工具.编辑器工具等,已经在多个实际项目(已上线)中使用.代码优雅 ...

随机推荐

  1. “ORA-01033:ORACLE initialization or shutdown in progress”错误的解决

    网页上显示以下错误信息: ORA-:ORACLE initialization or shutdown in progress 启动oracle数据库,有以下提示信息 Database mounted ...

  2. css中怎么设置透明度的问题

    小伙伴们是不是在找怎么样去设置页面的透明度的方法呢...别找了,我这儿就有,而且肯定够用了. 我自己会用到的就有两种,可以和大家分享一下. 1.用opcity的方法去设置透明度.代码如下: .div ...

  3. Log4j的ConversionPattern无缝适配到Logback

    为了能将log4j的ConversionPattern无缝应用到logback上来,需要对两个Conversion做适配,具体可以参考:Log4j 与 Logback的ConversionPatter ...

  4. SNMP–Simple Network Management Protocol

    I am planning to write an introduction and simple implementation of SNMP in software, when my work w ...

  5. 『TCP/IP详解——卷一:协议』读书笔记——10

    2013-08-22 22:57:17 3.8 ifconfig命令 这个命令在Linux系统下可以通过下面的指令阅读说明文档: ifconfig 由于书中作者用的系统比较早的某Unix系统,所以我的 ...

  6. [UE4]AnimDynamics简介

    AnimDynamics简介 Author:Jia Zhipeng AnimDynamics是UE4.11 Preview 5测试版本发布的AnimationBlueprint中的新节点.功能是通过简 ...

  7. c++中的指针之指针在数组

    使用一维指针数组输出一维数组中的数 int array[]={1,2,3,4,5,6};        int *p; p=array;        for(int i=0;i<6;i++){ ...

  8. html5 datepicke

    最近在看html5新特性,对于input中date类型的显示想自定义,但不知道如何解决!有没有关注过的或已解决的给提供个方案??静等佳音.

  9. Byte,TBytes,array of Byte, array[0..9] of byte的区别

    Byte前面已经说是存放bit的单元,是电脑内存的基本单位,byte表示0-255中的256个数字 下面为Byte的用法: var B: Byte; // 表示0-255的数字 begin B := ...

  10. xcode6如何支持空模板

    Single View Application 改成空模板的歩揍(xcode6.0.1): 1.删除info.plist 中的Main Storyboard file base name 选项 2.将 ...