//********************************************************
// The following code example is taken from the book
//  C++23 - The Complete Guide
//  by Nicolai M. Josuttis (www.josuttis.com)
//  https://www.cppstd23.com
//
// The code is licensed under a
//  Creative Commons Attribution 4.0 International License
//  https://creativecommons.org/licenses/by/4.0/
//********************************************************


#include <iostream>
#include <exception>
#include "genexcept.hpp"

int main()
{
  auto gen = coroWithException();

  std::cout << "begin(): " << std::endl;
  auto pos = gen.begin();                       // START coroutine => MAY THROW

  while (pos != gen.end()) {
    std::cout << "got: " << *pos << std::endl;
    std::cout << "++pos: " << std::endl;
    try {
      ++pos;                                    // RESUME coroutine => MAY THROW
    }
    catch (const std::exception& e) {           // handle exception from ++pos locally
      std::cout << "EXCEPTION: " << e.what() << std::endl;
    }
  }
  std::cout << "at end()" << std::endl;
}

