How do I use std::out_of_range ?

Discussion in 'C++' started by Steve555, Apr 25, 2007.

  1. Steve555

    Steve555 Guest

    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
     
    Steve555, Apr 25, 2007
    #1
    1. Advertisements

  2. 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::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?

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Apr 25, 2007
    #2
    1. Advertisements

  3. Steve555

    Steve555 Guest

    On 25 Apr, 18:09, Erik Wikström <> wrote:
    > 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::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?
    >
    > --
    > 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
     
    Steve555, Apr 26, 2007
    #3
  4. Steve555

    cyberscientist

    Joined:
    Apr 29, 2008
    Messages:
    1
    Likes Received:
    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 (Text):
    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:
    htmlcgi.cpp

    ViewAll.cpp:


    thanks to all in advance
     
    cyberscientist, Apr 29, 2008
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.