Throwing an Exception within a Lambda Expression - C++ STL

C++ examples for STL:Lambda

Description

Throwing an Exception within a Lambda Expression

Demo Code

#include <iostream>
#include <algorithm>
#include <vector>
#include <stdexcept>

using namespace std;

void ProcessVector(vector<int>& vect)
{
    sort(vect.begin(), vect.end(),[](int S1, int S2) throw(out_of_range)
         {//from  w w w. jav a 2 s.co  m
                 if (S1 > 100 || S2 > 100)
                    throw new out_of_range("Value over 100");

             return S1 < S2;
         });

    for_each(vect.begin(), vect.end(),
             [](int x) throw() {cout << x << endl;});
}

int main()
{
    vector<int> myV;
    myV.push_back(11);
    myV.push_back(2);
    myV.push_back(33);
    myV.push_back(4);

    //myV.push_back(101);

    ProcessVector(myV);
}

Result


Related Tutorials