Friday, 16 January 2015

STL Algorithm and Exceptional Handling

// stlCopyalgorithmostremiterator.C

#include <iostream>
#include<vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{

        vector <int> Integer(10,5);
        ostream_iterator< int > output(cout, " ");

        cout << "test to see if it works" << endl;
        copy(Integer.begin(), Integer.end(), output);

        return 0;
}


 //stlinputcopyiteratorwithCopy.C
#include <iostream>
#include<vector>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{

        vector <int> Integer(10);
        istream_iterator< int > input(cin);
        ostream_iterator< int > output(cout,  " ");

        cout << "test to see if it works" << endl;
        for(int i =0; i<10; i++)
        Integer.push_back(*input++);
        copy(Integer.begin(), Integer.end(), output);

        return 0;
}


// stackUnwinding.C
#include <iostream>
#include <stdexcept>
#include <exception>

using namespace std;
int function2() throw (runtime_error)
{
        cout << "inside function2" << endl;
        throw runtime_error(" Rahul testing stack Unwinding" );

        cout << "this should not be printed" << endl;
        return 0;
}
int function1 () throw (runtime_error)
{
        cout << "inside function1" << endl;
        function2();
        cout << "function1 this should not be displayed" << endl;
        return 0;
}

int main()
{

        try {
                function1 ();
                cout << "this should not be printed from main" << endl;
        }
        catch (runtime_error &errobj)
        {
                cout << "Caught" << errobj.what() << endl;
        }
return 0;
}

No comments:

Post a Comment