Given the basic code:
std::vector<int> foo(10);
foo.at[100] = 42;
This will throw an std::out_of_range
as expected, and the default behaviour on my machine is to print a string like:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 100) >= this->size() (which is 10)
If I wrap the code with a try/catch
:
try {
foo.at[100] = 42;
} catch (std::out_of_range e) {
printf("Error at %s line %d\n", __FILE__, __LINE);
}
I can of course get the line where the problem occurs, but if the code has tens of uses of at
and I do this:
try {
functionThatCallsAtFromLotsOfPlaces();
} catch (std::out_of_range e) {
printf("Error at %s line %d\n", __FILE__, __LINE);
}
I only get the line number of the catch
, not the throw
.
On the other hand, if I load up the program in gdb
and type catch throw
, gdb
will hand control back to me at the throw
, so I can easily trace where exactly the error is, not where it was caught. Is there a mechanism in C++11
, or gcc
-specific, where I can emulate what gdb
is doing?
Note, there is this question, but that is for signal errors; it doesn't work for me for normal throw
s.