using exceptions

T

Tony Johansson

Hello experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I find is wrong as I
believe.

An array index that is out of bounds in an array will not generate an
exception but will
generate the out_of_range exception if used in a vector(that is part of the
standard library)

Is this really true that out_of_range exception is generated if used in a
vector(that is part of the standard library)

I tried this but no exception was generated in this piece of code.
#inlude <vector>
#include <iostream>
int main()
{
try
{
vector<int> v(3);
cout << v[0];
cout << v[1];
cout << v[2];
cout << v[99];
}
catch (exception &e)
{
cout << exception occurred" << endl;
}
}

Many thanks

//Tony
 
P

Pete Becker

Tony said:
Is this really true that out_of_range exception is generated if used in a
vector(that is part of the standard library)

Nope. That's what happens if you use the member function
vector::at(size_type) instead of its operator[]. Another fundamental
mistake in that book.
 
P

peter.koch.larsen

Tony Johansson skrev:
Hello experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I find is wrong as I
believe.

Just one more reason to drop that book. It is obviously worthless.
An array index that is out of bounds in an array will not generate an
exception

This might be correct in practice, although the lawyer would note that
it is undefined behaviour in which case anything could happen.
but will
generate the out_of_range exception if used in a vector(that is part of the
standard library)

If you use "at", you will get the exception. Using operator[] it is
again undefined behaviour, but this time it is more likely that the
exception will be thrown. An implementation is allowed to check if you
index outside of the vector and throw - and there are libraries that
will do so in a debug build.
Is this really true that out_of_range exception is generated if used in a
vector(that is part of the standard library)

I tried this but no exception was generated in this piece of code.
#inlude <vector>
#include <iostream>
int main()
{
try
{
vector<int> v(3);
cout << v[0];
cout << v[1];
cout << v[2];
cout << v[99];
}
catch (exception &e)
{
cout << exception occurred" << endl;
}
}

It is okay not to throw - again: it is undefined behaviour.
Many thanks

//Tony

/Peter
 
S

Steffen

Tony said:
...
>
An array index that is out of bounds in an array will not generate an
exception but will
generate the out_of_range exception if used in a vector(that is part of the
standard library)

...

#inlude <vector>
#include <iostream>
int main()
{
try
{
vector<int> v(3);
cout << v[0];
cout << v[1];
cout << v[2];
cout << v[99];
}
catch (exception &e)
{
cout << exception occurred" << endl;
}
}


Hi,

out-of range check is only performed if you use at() to access elements
of vector, not with [], however.

Steffen
 
P

peter.koch.larsen

Steffen skrev:

[snip]
Hi,

out-of range check is only performed if you use at() to access elements
of vector, not with [], however.
That is not entirely true. An implementation is allowed to do
out-of-range checks also with operator[]. It is not required to,
however.

/Peter
 
B

benben

Steffen skrev:

[snip]
Hi,

out-of range check is only performed if you use at() to access elements
of vector, not with [], however.
That is not entirely true. An implementation is allowed to do
out-of-range checks also with operator[]. It is not required to,
however.

However, safe (in the sense that it throws exceptions when needed) STL
containers are one of the main sell points of the proposed C++0x...I'd
heard.

Ben
 
N

Nick Keighley

benben said:
Steffen skrev:
out-of range check is only performed if you use at() to access elements
of vector, not with [], however.
That is not entirely true. An implementation is allowed to do
out-of-range checks also with operator[]. It is not required to,
however.

However, safe (in the sense that it throws exceptions when needed) STL
containers are one of the main sell points of the proposed C++0x...I'd
heard.

I'm not sure what you are saying here. STL containers throw exceptions
when they are are specified to. Hence vector::at() throws if out of
range, vector::eek:perator[] has no defined behaviour when out of range.

Note that a "selling point" of the STL was efficiency.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top