-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy patherror_handling.cpp
More file actions
52 lines (42 loc) · 1.69 KB
/
error_handling.cpp
File metadata and controls
52 lines (42 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Copyright (c) 2016-2020 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/sml.hpp>
#include <cassert>
#include <iostream>
#include <stdexcept>
namespace sml = boost::sml;
namespace {
struct some_event {};
struct error_handling {
auto operator()() const {
using namespace sml;
// clang-format off
return make_transition_table(
*("idle"_s) + "event1"_e / [] { throw std::runtime_error{"error"}; }
, "idle"_s + "event2"_e / [] { throw 0; }
, *("exceptions handling"_s) + exception<std::runtime_error> / [] { std::cout << "exception caught" << std::endl; }
, "exceptions handling"_s + exception<_> / [] { std::cout << "generic exception caught" << std::endl; } = X
, *("unexpected events handling"_s) + unexpected_event<some_event> / [] { std::cout << "unexpected event 'some_event'" << std::endl; }
, "unexpected events handling"_s + unexpected_event<_> / [] { std::cout << "generic unexpected event" << std::endl; } = X
);
// clang-format on
}
};
} // namespace
int main() {
using namespace sml;
sm<error_handling> sm;
sm.process_event("event1"_e()); // throws runtime_error
assert(sm.is("idle"_s, "exceptions handling"_s, "unexpected events handling"_s));
sm.process_event("event2"_e()); // throws 0
assert(sm.is("idle"_s, X, "unexpected events handling"_s));
sm.process_event(some_event{}); // unexpected event
assert(sm.is("idle"_s, X, "unexpected events handling"_s));
sm.process_event(int{}); // unexpected any event
assert(sm.is("idle"_s, X, X));
}