You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
653 B
C++

// https://www.cs.uaf.edu/2010/spring/cs202/lecture/03_02_exceptions.html
#include <iostream>
using std::cout;
using std::endl;
void dr_evil(int counter) {
int *array = new int[100000000000/(1+10*counter)];
cout << "Bwah ha ha!" << endl;
delete[] array;
}
int foo(void) {
for (int counter = 0; counter < 20; counter++) {
try {
cout << "About to call dr_evil with counter == " << counter << endl;
dr_evil(counter);
cout << "I guess he's not so evil!" << endl;
break;
} catch (std::bad_alloc &e) {
cout << "Caught an exception: " << e.what() << endl;
}
}
return 0;
}
int main(void) {
foo();
}