throw.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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/continuation.hpp>
  11. namespace ctx = boost::context;
  12. struct my_exception : public std::runtime_error {
  13. ctx::continuation c;
  14. my_exception( ctx::continuation && c_, std::string const& what) :
  15. std::runtime_error{ what },
  16. c{ std::move( c_) } {
  17. }
  18. };
  19. int main() {
  20. ctx::continuation c = ctx::callcc([](ctx::continuation && c) {
  21. for (;;) {
  22. try {
  23. std::cout << "entered" << std::endl;
  24. c = c.resume();
  25. } catch ( my_exception & ex) {
  26. std::cerr << "my_exception: " << ex.what() << std::endl;
  27. return std::move( ex.c);
  28. }
  29. }
  30. return std::move( c);
  31. });
  32. c = c.resume_with(
  33. [](ctx::continuation && c){
  34. throw my_exception(std::move( c), "abc");
  35. return std::move( c);
  36. });
  37. std::cout << "main: done" << std::endl;
  38. return EXIT_SUCCESS;
  39. }