blob: 0a08790fe03d7fc134a52ce5a4daa960360c9a4c (
plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// { dg-do run { xfail sparc64-*-elf arm-*-pe } }
// { dg-options "-fexceptions" }
#include <cstdlib>
#include <iostream>
class Vector {
private:
int *p;
int sz;
public:
// Exception class
class Range {
private:
int value_i;
public:
Range( int i ) { value_i = i; };
int value() { return value_i; };
};
Vector( int s );
~Vector();
int size() { return sz; };
int& operator []( int i );
};
Vector::Vector(int s) {
sz = s;
p = new int[sz];
}
Vector::~Vector() {
delete [] p;
}
int&
Vector::operator [](int i) {
if (0<=i && i<sz) {
return p[i];
} else {
throw Range( i );
}
}
void crash(Vector& v ) {
v[v.size()+10]; // Triggers range error!
}
void do_something(Vector& v) {
crash( v );
}
void
f(Vector& v) {
try {
do_something( v );
} catch (Vector::Range& r) {
std::cout << "Invalid vector range " << r.value()
<< " caught in f()" << std::endl;
std::exit(0);
}
}
main() {
Vector v(10);
f( v );
return 1;
}
|