/*----
  signature: main: void -> int
  purpose: trying out C++ exception handling, 
      calling a statement that might throw an exception
      (modified version of Week 13 Lecture 1's exc1.cpp)

  compile using:
      g++ exc2.cpp -o exc2
  run using:
      ./exc2

  by: Sharon Tuttle
  last modified: 2022-11-17
----*/

#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <exception>
#include <vector>
using namespace std;

int main()
{
    int vector_size;
    vector<string> my_collection;
    
    // NOTE: this should be an if-statement,
    //    using exceptions as a demo-of-concept!!

    try
    {
        cout << "enter desired vector size: ";
        cin >> vector_size;

        // through some, ahem, trial-and-error,
        //    discovered that the following WILL
        //    throw an exception of type length_error
        //    if vector_size is less than 0

        my_collection = vector<string>(vector_size);

        // still a silly action to see if we get this
        //    far
        
        cout << "Silly: dividing 10 by vector size: "
             << (10 / vector_size) << endl;
    }
    catch (const length_error& e)
    {
        cout << "exception thrown: "
             << e.what() << endl
             << "...setting vector size to 0" << endl;
        
        vector_size = 0;
    }

    catch ( ... )
    {
        // why is this NOT reached for divide-by-0???

        cout << "reached none-of-the-above catch!" << endl;
    }
        
    cout << "my collection size: "
         << my_collection.size() << endl;

    return EXIT_SUCCESS;
}