sol2 icon indicating copy to clipboard operation
sol2 copied to clipboard

How to catch exceptions in Lua?

Open leelyn opened this issue 4 years ago • 2 comments

First of all, thank you for all of your great work on sol2 and the C and C++ standards committees!

My problem is that I don't know how to catch exceptions in Lua, is there something wrong with my operation?

C++ file

#define SOL_ALL_SAFETIES_ON 1
#include <sol/sol.hpp>
int lua_exception_handler(lua_State* L,
    sol::optional<const std::exception&> maybe_exception, sol::string_view description)
{
    if (maybe_exception)
    {
        // const std::exception& ex = *maybe_exception;
        // obe::Debug::Log->error("<LuaError>[Exception] : {}", ex.what());
        std::cout << "(straight from the exception): ";
		const std::exception& ex = *maybe_exception;
		std::cout << ex.what() << std::endl;
    }
    else
    {
        //obe::Debug::Log->error("<LuaError>[Error] : {}", description);
        std::cout << "(from the description parameter): ";
		std::cout.write(description.data(), static_cast<std::streamsize>(description.size()));
		std::cout << std::endl;
    }
    return sol::stack::push(L, description);
}

inline void lua_panic_handler(sol::optional<std::string> maybe_msg) {
	std::cerr << "Lua is in a panic state and will now abort() the application" << std::endl;
	if (maybe_msg) {
		const std::string& msg = maybe_msg.value();
		std::cerr << "\terror message: " << msg << std::endl;
	}
	// When this function exits, Lua will exhibit default behavior and abort()
}

int main()
{
	auto sol_state = new sol::state(sol::c_call<decltype(&lua_panic_handler), &lua_panic_handler>);
	sol_state->open_libraries(sol::lib::base,sol::lib::debug);

        sol_state->set_exception_handler(&lua_exception_handler);

	sol_state->safe_script_file("a.lua", &sol::script_pass_on_error);
}

a.lua file

"test error"  --Captured here, you can continue to run down
local btest = require("b")
btest:init()

b.lua file

local b = {}

function b:init()
	"test error"  --The exception cannot be caught here, and the program crashes, how to catch the exception here and let the program continue to execute
end

return b

leelyn avatar Nov 11 '21 18:11 leelyn

What version of lua are you running? Are you compiling it as C++? What compiler are you using?

When trying to reproduce this issue using the code you provided above with latest sol and Lua 5.4.2 on MSVC v142 (2019) the program just exits cleanly with nothing printed (as expected). Checking the result of safe_script_file as seen below produces the correct error:

sol::protected_function_result result = sol_state->safe_script_file("a.lua", &sol::script_pass_on_error);

if (!result.valid())
{
    sol::error error = result;
    std::cout << error.what() << std::endl;
}

Prints:

a.lua:1: unexpected symbol near '"test error"'

OrfeasZ avatar Nov 12 '21 20:11 OrfeasZ

What version of lua are you running? Are you compiling it as C++? What compiler are you using?

When trying to reproduce this issue using the code you provided above with latest sol and Lua 5.4.2 on MSVC v142 (2019) the program just exits cleanly with nothing printed (as expected). Checking the result of safe_script_file as seen below produces the correct error:

Thank you for your answer. lua v5.3,MSVC v142(2019) How to make the application report an error and not exit?

leelyn avatar Nov 15 '21 17:11 leelyn