#include <iostream>
#include <stdexcept>
using std::cout;
using std::cin;
using std::endl;
using std::runtime_error;
using std::invalid_argument;

class MyInteger
{
public:
    MyInteger(int n): value(n) {cout << "Constructor called for MyInteger "
        << n << ".\n";}
    ~MyInteger()
    { cout << "Destructor of MyInteger " << value << " called.\n"; }
private:
    int value;
};

class Object
{
public:
    Object(int n=0): value(n) 
    {
        MyInteger a(2019);
        if (n == 0)
        {
            cout << "=== Call exception handler ===\n";
            throw runtime_error("zero!");
        }
        else if (n < 0)
            throw invalid_argument("negative!");
    }
private:
    MyInteger value;
};

int main()
{
    int n;
    cout << "Input 1, 0 or -1: ";
    /* 1: Program ends normally
     * 0: Invoke an exception handler
     * -1: No corresponding handler.  Program terminates.
     */
    cin >> n;
    try {
    Object b(n);
    } catch (runtime_error& e) {
        cout << e.what() << endl;
        cout << "Exception handled.\n";
    }
    return 0;
}
