none
Visual C++ Try Catch Exception block not entered RRS feed

  • Question

  • Hi, I'm trying to catch an STL "out_of_range" exception thrown by accessing one-pass-the-end element, but this is not being caught in my exe both "Debug" or "Release" mode. Instead I get a pop-up:

    "Debug Assertion Failed! ....Expression: vector subscript out of range... Press Retry to debug the application [Abort][Retry][Ignore]"

    This is not the behaviour I expected, instead I wanted the catch-block to be entered and my error message to be displayed and the program would carry on, as per normal try-catch behaviour. Here's my code (I'm using the free Visual C++ 2008):

    #include <iostream>
    #include <vector>

    int main()
    {
        using namespace std;
        vector<int> v;
        int x;
       
        cout << "\nFill vector with integers, type -1 to stop:" << endl;
        while(cin>>x && x!=-1)
            v.push_back(x);

        try
        {
            for(unsigned int i=0; i<=v.size(); ++i)
                cout << "\nv[" << i << "] == " << v[i];
        }
        catch(exception& oe)
        {
            cerr << "\nError " << oe.what();
        }
        catch(...)
        {
            cerr << "\nError ";
        }
           
        return 0;
    }

    Any clues as to how I get the normal behaviour out of the exe? Thanks.
    Sunday, May 10, 2009 7:35 PM

Answers

  • This is normal behavior.  For performance reasons, vector<> doesn't provide range checking, so no exception is thrown.  (though when compiled in debug mode, an assertion is triggered).

    If you want range checking, use vector's "at" member instead, and catch: std::out_of_range outOfRange, e.g.:

    try
    {
          for(unsigned int i=0; i<=v.size(); ++i)
              cout << "\nv[" << i << "] == " << v.at(i);
    }
    catch ( std::out_of_range outOfRange ) 
    {
          cout << "\n\nException: " << outOfRange.what();
    }


    Michael Asher
    • Proposed as answer by masher2 Monday, May 11, 2009 11:38 AM
    • Marked as answer by ElectroZap Monday, May 11, 2009 1:32 PM
    Sunday, May 10, 2009 9:46 PM

All replies

  • This is normal behavior.  For performance reasons, vector<> doesn't provide range checking, so no exception is thrown.  (though when compiled in debug mode, an assertion is triggered).

    If you want range checking, use vector's "at" member instead, and catch: std::out_of_range outOfRange, e.g.:

    try
    {
          for(unsigned int i=0; i<=v.size(); ++i)
              cout << "\nv[" << i << "] == " << v.at(i);
    }
    catch ( std::out_of_range outOfRange ) 
    {
          cout << "\n\nException: " << outOfRange.what();
    }


    Michael Asher
    • Proposed as answer by masher2 Monday, May 11, 2009 11:38 AM
    • Marked as answer by ElectroZap Monday, May 11, 2009 1:32 PM
    Sunday, May 10, 2009 9:46 PM
  • The debugging code in the STL jerks the emergency chord before you'll get the exception.  Turn it off by putting this in your precompiled header file:

    #define _SECURE_SCL 0
    #define _HAS_ITERATOR_DEBUGGING 0
    #include <vector>

    Think about the loss of diagnostics you'll have when doing this.  Catching the exception is also a doubtful feature.  Indexing an array out of bounds is a bug.  Fix the bug, don't fix the message.

    Hans Passant.
    Sunday, May 10, 2009 10:59 PM
    Moderator