Exception handling
Feb 20, 2013 at 7:24pm UTC
So, I'm working on Exception handling exercises and the out_of_range exception isn't catching the runtime error when I'm exceeding the range of vector v. Am I doing it ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
void error(string err)
{
throw runtime_error(err);
}
int main()
{
try {
vector<double >v;
double x;
while (cin>>x)
{
if (x == 00)
break ;
v.push_back(x);
}
if (!cin) error("couldn't read a double for input" );
for (unsigned int i=0;i<=v.size();++i)
cout<<"v[" <<i<<"]==" <<v[i]<<endl;
}
catch (exception& e)
{
cerr<<"Runtime error: " <<e.what()<<"\n" ;
cin.get();
return 1;
}
catch (...)
{
cerr<<"OOpsie! Unknown exception!\n" ;
cin.get();
return 2;
}
}
Feb 20, 2013 at 7:41pm UTC
There won't be an out of range exception thrown. You aren't using checked access.
Change line 26 to:
cout<<"v[" <<i<<"]==" <<v.at(i)<<endl;
Feb 20, 2013 at 7:52pm UTC
Aah, it tells that it requires library support from upcoming ISO C++
I added -std=c++0x to my links but it's not fixing it. Is there a new library I need to get a hold of?
Topic archived. No new replies allowed.