throw.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright Oliver Kowalke 2016.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cstdlib>
  6. #include <exception>
  7. #include <iostream>
  8. #include <stdexcept>
  9. #include <string>
  10. #include <boost/context/fiber.hpp>
  11. namespace ctx = boost::context;
  12. struct my_exception : public std::runtime_error {
  13. ctx::fiber f;
  14. my_exception( ctx::fiber && f_, std::string const& what) :
  15. std::runtime_error{ what },
  16. f{ std::move( f_) } {
  17. }
  18. };
  19. int main() {
  20. ctx::fiber f{[](ctx::fiber && f) ->ctx::fiber {
  21. std::cout << "entered" << std::endl;
  22. try {
  23. f = std::move( f).resume();
  24. } catch ( my_exception & ex) {
  25. std::cerr << "my_exception: " << ex.what() << std::endl;
  26. return std::move( ex.f);
  27. }
  28. return {};
  29. }};
  30. f = std::move( f).resume();
  31. f = std::move( f).resume_with([](ctx::fiber && f) ->ctx::fiber {
  32. throw my_exception(std::move( f), "abc");
  33. return {};
  34. });
  35. std::cout << "main: done" << std::endl;
  36. return EXIT_SUCCESS;
  37. }