Exception

why cant catch my int's throw?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vettore <int> v;

v.push_back(8);
v.push_back(4);

try
     {
          for(int i = 0; i < 2; i++)
          {    
              cout << v[i] <<endl; 
          }
          
          double c = 5.25;
          throw v[2];  
          float a = 0; 
     }
     catch (int) 
     {
          cerr << "Error: READING OVER THE MAXIMUM DIMENSION" <<endl;    
     }
     catch (...)
     {
          cerr << "UNEXPECTED ERROR" <<endl; 
     }
Last edited on
Accessing an element that does not exist with the subscript operator results in undefined behavior. You may get a segfault if you are lucky, or if you are unlucky you may get a garbage value. You seem not to understand how to use exceptions - why are you trying to throw and catch a nonexistant element?
yes yes i'm then able to understand it alone xD
Topic archived. No new replies allowed.