sol2
sol2 copied to clipboard
Sometimes it is not possible to catch sol::error
My IDE: Microsoft Visual Studio Community 2022 v17.4.5
The code 1:
#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>
int main() {
sol::state lua;
try {
sol::function f = lua[""];
f();
}
catch (const sol::error& e) {
std::cout << e.what() << std::endl;// Yes,I catched it.
}
return 0;
}
The code 2:
#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>
int main() {
sol::state lua;
try {
sol::table t = lua[""];// An uncatchable error was raised here.
}
catch (const sol::error& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
I tried the codes in godbolt and the first code does not throw at all.
The second code throws. lua: error: stack index -1, expected table, received nil: value is not a table or a userdata that can behave like one (type check failed in constructor)
GCC 10.1, Lua 5.3.5, Sol 3.2.1
So sounds like both codes may work differently in different environments. Or maybe just different sol or lua versions handle the cases differently,
- https://godbolt.org/z/6anYEq6h5
- https://godbolt.org/z/ad7frnrEY
@Rochet2
I have provided a screen recording to reproduce the bug:

This is my code:
#define SOL_ALL_SAFETIES_ON 1
#define SOL_SAFE_FUNCTIONS 0
#include <sol/sol.hpp>
#include <iostream>
int main() {
std::cout << SOL_VERSION << std::endl;
std::cout << LUA_VERSION << std::endl;
sol::state lua;
try {
sol::function f = lua[""];
f();
}
catch (const sol::error& e) {
std::cout << e.what() << std::endl;
}
try {
sol::table t = lua[""];
}
catch (const sol::error& e) {
std::cout << e.what() << std::endl;
}
return 0;
}