With some test code similar to that below, I can't seem to catch the exception that is thrown on the
v1[44]... line.

What should I check to solve this problem? (I am using a mixture of MFC and STL.)

vector<int> v1;
try
{
v1[44] = 22;
}
catch(std::exception e)
{
e.what();
e.what();
}

Thanks,

Frank

Re: VC++2008 and vector exception - not caught by Alf

Alf
Sat Jul 19 18:39:47 CDT 2008

* Frank S:
>
> With some test code similar to that below, I can't seem to catch the
> exception that is thrown on the v1[44]... line.

You're not guaranteed that there will be an exception, just Undefined Behavior
(which might do anything or nothing).



> What should I check to solve this problem? (I am using a mixture of MFC
> and STL.)

As I recall there is, as you imply, a Visual C++ option to get automatic
checking also for operator[] -- if so then it's presumably documented.

However, why don't you instead make that explicit in the code, and write

v.at(44) = 22;

Then, for a standard-conforming compiler, you get a std::something exception
derived from std::exception.


> vector<int> v1;
> try
> {
> v1[44] = 22;
> }
> catch(std::exception e)

For efficiency, safety, and not the least not making other programmers waste
time on figuring out "why did he do /that/", consider catching in the idiomatic
way, by reference to const:

catch( std::exception const& e )



> {
> e.what();
> e.what();
> }

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Re: VC++2008 and vector exception - not caught by Doug

Doug
Sat Jul 19 18:44:15 CDT 2008

On Sat, 19 Jul 2008 18:31:08 -0500, Frank S <OldGrouch@community.nospam>
wrote:

>
>With some test code similar to that below, I can't seem to catch the exception that is thrown on the
>v1[44]... line.
>
>What should I check to solve this problem? (I am using a mixture of MFC and STL.)
>
> vector<int> v1;
> try
> {
> v1[44] = 22;
> }
> catch(std::exception e)
> {
> e.what();
> e.what();
> }

The vector::operator[] does not validate its parameter. Therefore, what
you're doing is undefined, and if it throws any kind of exception, it's
going to be the Windows structured kind, probably an access violation. You
can use vector::at instead of vector::operator[] if you want parameter
validation; v.at(n) throws std::out_of_range for n >= v.size().

--
Doug Harrison
Visual C++ MVP

Re: VC++2008 and vector exception - not caught by Frank

Frank
Sat Jul 19 19:09:01 CDT 2008

Thank you for the help Alf.

I found that:

v.at(44) = 22;

throws an exception as you suggested, and

v[44] = 22;

does not (although in debug builds the error is reported).

Thanks again,

Frank

Alf P. Steinbach wrote:
> * Frank S:
>> With some test code similar to that below, I can't seem to catch
>> the exception that is thrown on the v1[44]... line.
>
> You're not guaranteed that there will be an exception, just Undefined
> Behavior (which might do anything or nothing).
>
>
>
>> What should I check to solve this problem? (I am using a mixture of
>> MFC and STL.)
>
> As I recall there is, as you imply, a Visual C++ option to get automatic
> checking also for operator[] -- if so then it's presumably documented.
>
> However, why don't you instead make that explicit in the code, and write
>
> v.at(44) = 22;
>
> Then, for a standard-conforming compiler, you get a std::something
> exception derived from std::exception.
>
>
>> vector<int> v1;
>> try
>> {
>> v1[44] = 22;
>> }
>> catch(std::exception e)
>
> For efficiency, safety, and not the least not making other programmers
> waste time on figuring out "why did he do /that/", consider catching in
> the idiomatic way, by reference to const:
>
> catch( std::exception const& e )
>
>
>
>> {
>> e.what();
>> e.what();
>> }
>
> Cheers, & hth.,
>
> - Alf
>

--
Frank

Re: VC++2008 and vector exception - not caught by jetan

jetan
Sun Jul 20 21:51:21 CDT 2008

Hi Frank,

Thanks for your feedback.

As Doug pointed, the main difference between vector::[] operator and
vector::at() function is that former one will not perform the
range/boundary check or validation. So vector::[] operator will not try to
throw standard C++ exception for invalid index. It will just use it for
referencing which may result in OS/Hardware level exception(such as Access
Violation) which will not be caught by C++ catch(std::exception). For this
type of exception, OS SEH(structured exception handling) is needed.

Yes, using vector::at() should be more conforming to C++ exception.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.


Re: VC++2008 and vector exception - not caught by Doug

Doug
Sun Jul 20 23:11:28 CDT 2008

On Mon, 21 Jul 2008 02:51:21 GMT, jetan@online.microsoft.com ("Jeffrey
Tan[MSFT]") wrote:

>Hi Frank,
>
>Thanks for your feedback.
>
>As Doug pointed, the main difference between vector::[] operator and
>vector::at() function is that former one will not perform the
>range/boundary check or validation. So vector::[] operator will not try to
>throw standard C++ exception for invalid index. It will just use it for
>referencing which may result in OS/Hardware level exception(such as Access
>Violation) which will not be caught by C++ catch(std::exception). For this
>type of exception, OS SEH(structured exception handling) is needed.

Just to be clear on the SEH angle, we really do only mean "may result". In
many cases, erroneous indexing will land in accessible memory that is not
part of the vector storage, and the program won't crash right away or
perhaps not at all. You might go on to read some random value, or you might
write to the bogus item and corrupt some other data structure. That said, I
wouldn't want anyone to think I'm arguing for vector::at; in fact, I can't
remember ever using the function.

--
Doug Harrison
Visual C++ MVP