How do I use std::out_of_range ?

S

Steve555

Hi,

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

terminate called after throwing an instance of 'std::eek:ut_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 == '[' && 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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi,

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

terminate called after throwing an instance of 'std::eek:ut_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 == '[' && 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 <stdexcept> and 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::eek:ut_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?
 
S

Steve555

In a function that erases part of a string, the compiler sometimes
gives this error:
terminate called after throwing an instance of 'std::eek:ut_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 == '[' && 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 <stdexcept> and 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::eek:ut_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?


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
 
Joined
Apr 29, 2008
Messages
1
Reaction score
0
Get this error During Run time

Hi ALL this might be out o date.

My Code compiles Fine But get this Error when i run my code.
I am Using Multi file programming, so here are my codes:

Make File:
Code:
COMPILER = g++
htmlcgi.o: htmlcgi.cpp htmlcgi.h

	$(COMPILER) –c htmlcgi.cpp
 
ViewAll: ViewAll.cpp htmlcgi.o

	$(COMPILER) -o ViewAll.cgi ViewAll.cpp htmlcgi.o

htmlcgi.h:
#include <cstdlib>

#include <iostream>

#include <fstream>
#include <string>
#ifndef htmlcgi
#define htmlcgi
//using std::string;
using std::ifstream;
using namespace std;

//=======================
//Declaring variables
//=======================

static string line;

static string rtrn;

static string chk;

static string fin= "---";





static string prodType[100];

static string stockLvl[100];

static string prodReO[100];

static string prodAlt[100];

static int N=100;

static int n=0;
static string prodRef[100];
static double prodPrc[100];
static string prodName[100];

//=======================
//Declaring Functions
//=======================

void stringCutter(const string& line, string& rtrn, int start, int length); //Cuts up the string inputed
void inputdata(); //reads the input file


#endif // htmlcgi
htmlcgi.cpp
#include <cstdlib>

#include <iostream>

#include <fstream>

#include <string>
#include <stdexcept>
#include "htmlcgi.h"
using namespace std;








//===================
//Read file
//===================
void stringCutter(const string& line, string& rtrn, int start, int length){



string snippet(line, start, length);

rtrn = snippet;

}

//===================
//Read file
//===================
void inputdata()
{

// Importing Data...

ifstream input;



// input.open(PATH);

input.open("ele205-2007.data");

try {
/* Code that can throw */


if (input.is_open()){



for (int i=0; i<N ; i++){



getline(input,line,'\n');



stringCutter(line, rtrn, 0, 55); prodName = rtrn;

stringCutter(line, rtrn, 56, 3); prodType = rtrn;

stringCutter(line, rtrn, 60, 4); prodRef = rtrn;

stringCutter(line, rtrn, 65, 6); prodPrc = atof(rtrn.c_str());

stringCutter(line, rtrn, 72, 4); stockLvl = rtrn;

stringCutter(line, rtrn, 77, 3); prodReO = rtrn;

stringCutter(line, rtrn, 81, 4); prodAlt = rtrn;



stringCutter(line, chk, 81, 3);




if (chk == fin){N=i;}//else{n++;}

}

}
}
catch (std::eek:ut_of_range& e) {
std::cout << "Out of range: " << e.what() << "\n";
}
catch (std::exception& e) {
std::cout << "Some other exception: " << e.what() << "\n";
}


input.close();
}



ViewAll.cpp:
//==============================================================================

//File Author: cyberscientist

//File Name: ViewAll.cpp

//File description Generates list of all Products

//==============================================================================





#include <cstdlib>

#include <iostream>

#include <fstream>

#include <string>
#include "htmlcgi.h"

//#include <sstream.h>



using namespace std;



// setting function to cut up string...

void stringCutter(const string& line, string& rtrn, int start, int length);



int main(){



// Setting variables...





// Importing Data...

inputdata();



// Output data...



cout << "Content-Type: text/html\n\n";

cout << "<html>\n<head>\n<title>Product List</title>\n</head>\n\n";

cout << "<body>\n\n";

cout << " <table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"5\">\n";



cout << " <tr>\n";



cout << " <td><h3>Product Name</h3></td> ";

cout << "<td><h3>Price</h3></td>\n";



cout << " </tr>\n";



cout.setf( ios::fixed , ios::floatfield );

cout.precision(2);



for(int i=0; i<N; i++){



cout << " <tr>\n";



cout << " <td> <a href=\"http://cgi.student.elec.qmul.ac.uk/~ap05017/cgi-bin/prodInfo.cgi?";

cout << "ref=" << prodRef << "\">" << prodName << "</a></td> ";

cout << "<td align=\"right\">" << prodPrc << "</td>\n";



cout << " </tr>\n";

}



cout << " </table>\n\n</body>\n</html>\n";



//cin >> fin;

return EXIT_SUCCESS;

}


thanks to all in advance
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top