MCPcopy Create free account
hub / github.com/boostorg/leaf / main

Function main

example/exception_to_result.cpp:56–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

54
55
56int main()
57{
58 // Exercise print_answer a few times and handle errors. Note that the
59 // exception objects that compute_answer_throws throws are not handled as
60 // exceptions, but as regular LEAF error error objects...
61 for( int i=0; i!=42; ++i )
62 {
63 leaf::try_handle_all(
64 []() -> leaf::result<void>
65 {
66 BOOST_LEAF_CHECK(print_answer());
67 return { };
68 },
69
70 []( error_a const & )
71 {
72 std::cerr << "Error A!" << std::endl;
73 },
74
75 []( error_b const & )
76 {
77 std::cerr << "Error B!" << std::endl;
78 },
79
80 // ...except for error_c errors, which (for demonstration) are
81 // captured as exceptions into std::exception_ptr as "unknown"
82 // exceptions. Presumably this should not happen, therefore at this
83 // point we treat this situation as a logic error: we print
84 // diagnostic information and bail out.
85 []( std::exception_ptr const * ep )
86 {
87 std::cerr << "Got unknown error!" << std::endl;
88
89 // Above, why do we take ep as a pointer? Because handle_all
90 // requires that the last handler matches any error and, taken
91 // as a pointer, if there isn't a std::exception_ptr associated
92 // with the error, the handler will still be matched (with 0
93 // passed for ep). Had we taken it by value or by const &, the
94 // program would not have compiled.
95 if( ep )
96 leaf::try_catch(
97 [&]
98 {
99 std::rethrow_exception(*ep);
100 },
101 []( leaf::error_info const & unmatched )
102 {
103 std::cerr << unmatched;
104 } );
105 } );
106 }
107
108 return 0;
109}

Callers

nothing calls this directly

Calls 3

try_handle_allFunction · 0.85
print_answerFunction · 0.85
try_catchFunction · 0.85

Tested by

no test coverage detected