MCPcopy Create free account
hub / github.com/ThePhD/sol2 / main

Function main

examples/source/docs/state_script_safe.cpp:6–44  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4#include <iostream>
5
6int main() {
7
8 std::cout << "=== safe_script usage ===" << std::endl;
9
10 sol::state lua;
11 // uses sol::script_default_on_error, which either panics or
12 // throws, depending on your configuration and compiler
13 // settings
14 try {
15 auto result1 = lua.safe_script("bad.code");
16 }
17 catch (const sol::error& e) {
18 std::cout << "an expected error has occurred: "
19 << e.what() << std::endl;
20 }
21
22 // a custom handler that you write yourself
23 // is only called when an error happens with loading or
24 // running the script
25 auto result2 = lua.safe_script("123 bad.code",
26 [](lua_State*, sol::protected_function_result pfr) {
27 // pfr will contain things that went wrong, for
28 // either loading or executing the script the user
29 // can do whatever they like here, including
30 // throw. Otherwise...
31 sol::error err = pfr;
32 std::cout
33 << "An error (an expected one) occurred: "
34 << err.what() << std::endl;
35
36 // ... they need to return the
37 // protected_function_result
38 return pfr;
39 });
40
41 std::cout << std::endl;
42
43 return 0;
44}

Callers

nothing calls this directly

Calls 2

safe_scriptMethod · 0.80
whatMethod · 0.80

Tested by

no test coverage detected