#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include <lauxlib.h>
};
int pcall_callback_err_fun(lua_State* L)
{
lua_Debug debug= {};
int ret = lua_getstack(L, 2, &debug); // 0是pcall_callback_err_fun自己, 1是error函数, 2是真正出错的函数
lua_getinfo(L, "Sln", &debug);
std::string err = lua_tostring(L, -1);
lua_pop(L, 1);
std::stringstream msg;
msg << debug.short_src << ":line " << debug.currentline;
if (debug.name != 0) {
msg << "(" << debug.namewhat << " " << debug.name << ")";
}
msg << " [" << err << "]";
lua_pushstring(L, msg.str().c_str());
return 1;
}
int main()
{
lua_State * L = luaL_newstate(); //创建lua运行
Lua的pcall错误处理
最新推荐文章于 2023-09-13 20:42:38 发布
该博客介绍了如何在C++中使用Lua的pcall函数进行错误处理。通过pcall_callback_err_fun回调函数,捕获并打印出Lua脚本执行过程中的错误信息。示例代码展示了如何加载并执行lua文件,以及在遇到错误时如何利用lua_pcall的错误处理机制。
摘要由CSDN通过智能技术生成