473,408 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

using exceptions

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
Aug 11 '05 #1
6 3936
Tony Johansson wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 11 '05 #2

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

Aug 11 '05 #3
Tony Johansson wrote:
...

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

Aug 11 '05 #4

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 Steffen


Aug 11 '05 #5

<pe***************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...

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


/Peter
Steffen

Aug 12 '05 #6
benben wrote:
<pe***************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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::operator[] has no defined behaviour when out of range.

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

Aug 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ricardo Jasinski | last post by:
Hello people!! I'm trying to improve an application I recently developed, in order to release its version 2.0. One of my concerns is to make it more robust, stable and with error-recovering...
4
by: Zork | last post by:
Hi, I am trying to stop object creation (in this case ill use a ball as the object) via use of exceptions. In essence, if the ball does not have an owner, I do not want the ball object created....
5
by: Miyra | last post by:
Hi. I'm working with an app that uses exceptions for control flow. These are code blocks where exceptions are thrown/caught regularly. A couple hundred exceptions occur per hour and they're caught...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
1
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I...
4
by: arrastogi71 | last post by:
Kindly tell me the costs and benefits of using exceptions?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.