exception.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright Oliver Kowalke 2009.
  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 <boost/coroutine/all.hpp>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <stdexcept>
  9. #include <string>
  10. #include <boost/bind.hpp>
  11. #include <boost/throw_exception.hpp>
  12. typedef boost::coroutines::asymmetric_coroutine< int >::pull_type pull_coro_t;
  13. typedef boost::coroutines::asymmetric_coroutine< int >::push_type push_coro_t;
  14. struct my_exception : public std::runtime_error
  15. {
  16. my_exception( std::string const& str) :
  17. std::runtime_error( str)
  18. {}
  19. };
  20. void echo( push_coro_t & sink, int j)
  21. {
  22. for ( int i = 0; i < j; ++i)
  23. {
  24. if ( i == 5) boost::throw_exception( my_exception("abc") );
  25. sink( i);
  26. }
  27. }
  28. int main( int argc, char * argv[])
  29. {
  30. pull_coro_t source( boost::bind( echo, _1, 10) );
  31. try
  32. {
  33. while ( source)
  34. {
  35. std::cout << source.get() << std::endl;
  36. source();
  37. }
  38. }
  39. catch ( my_exception const& ex)
  40. { std::cout << "exception: " << ex.what() << std::endl; }
  41. std::cout << "\nDone" << std::endl;
  42. return EXIT_SUCCESS;
  43. }