473,500 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I use std::out_of_range ?

Hi,

In a function that erases part of a string, the compiler sometimes
gives this error:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
I can guess, then, that I'm erasing outside the strings length, but I
can not find the bug:

for(long i=0; i<str.length()-1; i++)
{
if( str[i] == '[' && str[i+1] == ']' )
str.erase(i, 2);
}

Maybe someone can see the problem in this case, but I'm also
interested in generally learning how to use these exceptions.
How do I use 'out_of_range' and 'what' in my source code so I can put
a breakpoint in to the debugger when this error occurs?

Thanks

Steve

Apr 25 '07 #1
2 22802
On 2007-04-25 11:34, Steve555 wrote:
Hi,

In a function that erases part of a string, the compiler sometimes
gives this error:

terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
I can guess, then, that I'm erasing outside the strings length, but I
can not find the bug:

for(long i=0; i<str.length()-1; i++)
{
if( str[i] == '[' && str[i+1] == ']' )
str.erase(i, 2);
}

Maybe someone can see the problem in this case, but I'm also
interested in generally learning how to use these exceptions.
How do I use 'out_of_range' and 'what' in my source code so I can put
a breakpoint in to the debugger when this error occurs?
out_of_range is one of the exceptions found in <stdexceptand is a
class derived from std::exception (found in <exception>). To handle
exceptions you put the stuff that might throw in a try-block and then
use catch() to catch the exceptions:

try {
/* Code that can throw */
}
catch (std::out_of_range& e) {
std::cout << "Out of range: " << e.what() << "\n";
}
catch (std::exception& e) {
std::cout << "Some other exception: " << e.what() << "\n";
}

This will first see if it's a out_of_range exception that has been
thrown, and if it is it will be handled as such. If not it will try to
catch it as a std::exception and handle it as such.

Notice though that there's no need for an exception to be derived from
std::exception, anything can be thrown, even ints doubles and such. You
should really read up on them in your book, there's more to know.

BTW, what debugger are you using that does not break on an unhandled
exception?

--
Erik Wikström
Apr 25 '07 #2
On 25 Apr, 18:09, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-04-25 11:34, Steve555 wrote:
Hi,
In a function that erases part of a string, the compiler sometimes
gives this error:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase
I can guess, then, that I'm erasing outside the strings length, but I
can not find the bug:
for(long i=0; i<str.length()-1; i++)
{
if( str[i] == '[' && str[i+1] == ']' )
str.erase(i, 2);
}
Maybe someone can see the problem in this case, but I'm also
interested in generally learning how to use these exceptions.
How do I use 'out_of_range' and 'what' in my source code so I can put
a breakpoint in to the debugger when this error occurs?

out_of_range is one of the exceptions found in <stdexceptand is a
class derived from std::exception (found in <exception>). To handle
exceptions you put the stuff that might throw in a try-block and then
use catch() to catch the exceptions:

try {
/* Code that can throw */}

catch (std::out_of_range& e) {
std::cout << "Out of range: " << e.what() << "\n";}

catch (std::exception& e) {
std::cout << "Some other exception: " << e.what() << "\n";

}

This will first see if it's a out_of_range exception that has been
thrown, and if it is it will be handled as such. If not it will try to
catch it as a std::exception and handle it as such.

Notice though that there's no need for an exception to be derived from
std::exception, anything can be thrown, even ints doubles and such. You
should really read up on them in your book, there's more to know.

BTW, what debugger are you using that does not break on an unhandled
exception?

--
Erik Wikström
Thanks Erik, I got that working as needed ( I had to include stdexcept
too).
It is something I need to read up on, but at least it's not a complete
mystery now that I have try&catch working

I'm using Apple's XCode. It does indeed break on exceptions, what I
shopuld have said was that I wanted to put some extra code in thheir
to try and trace this bug that happens so rarely I couldn't , er,
'catch' it.

Steve

Apr 26 '07 #3

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

Similar topics

1
1849
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
13
5056
by: Mike Austin | last post by:
Hi all. Just working on a small virtual machine, and thought about using vector iterators instead of pointer arithmetic. Question is, why does an iterator plus any number out of range not...
8
5119
by: puzzlecracker | last post by:
Does string class take into consideration a poterntial buffer overflow issue? or does std:string::c_str() member functions does? what are the preventives?
4
11210
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and...
17
14639
by: desktop | last post by:
I have the following code: #include <stdexcept> int main(){ int i = 10; int arr; int index = 11;
8
14621
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
3
2226
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so,...
5
18380
MrPickle
by: MrPickle | last post by:
I googled and got some answers but whenever I used it I kept getting and std::out_of_range exception and I'm not completely clear on how to use it. I know how to initialize it; std::vector<...
1
11876
by: orSp4n | last post by:
I have a program that compiles and runs fine under Win XP. (using DevC++ beta 5). I've compiled the same program under Linux (Debian Lenny) and Mac OS X 10.5.8. (Using Eclipse). However when I...
0
7027
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7215
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
7245
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...
1
6919
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
3115
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3113
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
320
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.