Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / context / example / execution_context_v2 / throw.cpp
1
2 //          Copyright Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <cstdlib>
8 #include <exception>
9 #include <iostream>
10 #include <stdexcept>
11 #include <string>
12
13 #include <boost/context/execution_context.hpp>
14
15 namespace ctx = boost::context;
16
17 struct my_exception : public std::runtime_error {
18     my_exception( std::string const& what) :
19         std::runtime_error{ what } {
20     }
21 };
22
23 int main() {
24     ctx::execution_context< void > ctx([](ctx::execution_context<void> && ctx) {
25         for (;;) {
26             try {
27                     std::cout << "entered" << std::endl;
28                     ctx = ctx();
29             } catch ( ctx::ontop_error const& e) {
30                 try {
31                     std::rethrow_if_nested( e);
32                 } catch ( my_exception const& ex) {
33                     std::cerr << "my_exception: " << ex.what() << std::endl;
34                 }
35                 return e.get_context< void >();
36             }
37         }
38         return std::move( ctx);
39     });
40     ctx = ctx();
41     ctx = ctx();
42     ctx = ctx( ctx::exec_ontop_arg, []() { throw my_exception("abc"); });
43
44     std::cout << "main: done" << std::endl;
45
46     return EXIT_SUCCESS;
47 }