5

I wrote some code that encounters an exception if the given user input is invalid so I put it in a try/catch block yet it still threw an exception. The code itself is quite long so here's a simplified version of the code that also encounters the exception. The exception itself is clear, the position "3" doesn't exist so naturally it would throw an exception, but it's inside of a try/catch block so it should get caught, but it doesn't.

int main() {
    try
    {
        vector<string> test = vector<string>{ "a","b","c" };
        string value = test[3];
    }
    catch (...)
    {

    }
}

Running this code just results in the following exception regardless of whether or not it's in a try/catch block.

Exception

I also tried specifying the exception (const out_of_range&e) but that didn't help either. It just caused the exact same exception.

int main() {
    try
    {
        vector<string> test = vector<string>{ "a","b","c" };
        string value = test[3];
    }
    catch (const out_of_range&e)
    {

    }
}

I'm using Visual Studio, could this be an issue with the IDE or the compiler it uses?

Shayna
  • 334
  • 5
  • 17
  • Element 3 is not member of your vector. Use 2 instead. – Raindrop7 Dec 23 '17 at 23:27
  • 2
    There are different types of *exceptions* om computer systems. You can only catch those that are thrown from C++ itself, not those that are caused by software, operating system or hardware errors. A crash like that you need to [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Dec 23 '17 at 23:28
  • 1
    As for the reason behind your *crash* you should go back to your [beginners books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and review them. – Some programmer dude Dec 23 '17 at 23:29
  • 1
    I know the reason behind the crash, this is example code to cause the out of range exception to showcase how the exception isn't caught as stated in my post "The exception itself is clear, the position "3" doesn't exist so naturally it would throw an exception, but it's inside of a try/catch block so it should get caught, but it doesn't." – Shayna Dec 23 '17 at 23:38
  • 2
    No, using `operator[]` will *not* throw an exception using standard C++. The Visual C++ runtime library might do that in a debug build, but that's a non-standard extension. And it might also throw up a dialog when the exception is thrown. If you let the program continue the `catch` might catch it. Visual C++ is kind of weird sometimes. – Some programmer dude Dec 23 '17 at 23:44
  • @Shayna -- That is **not** an exception. The Visual Studio debug runtime did a check on your `operator []` using `assert()`, and your out-of-bounds access triggered the assertion. If you want to see it in action, put in your code `assert(1 == 2);` -- you should see the box pop-up, but with different text describing the assertion. – PaulMcKenzie Dec 24 '17 at 00:18

3 Answers3

10

If you want std::vector to throw an std::out_of_range exception, you need to use the .at() method. The operator[] does not throw an exception.

For example, you can do something like this:

std::vector<int> myvector(10);
try {
    myvector.at(20)=100;      // vector::at throws an out-of-range
}
catch (const std::out_of_range& e) {
    std::cerr << "Out of Range error: " << e.what() << '\n';
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Justin Randall
  • 2,243
  • 2
  • 16
  • 23
8

That is not an exception. That is a debug assert failure.

If you want an exception you need to use vector's at(index) function and not the array subscript operator.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
1

operator[] is overloaded in the vector container but is not exception safe (Behaviour is undefined in case of failure, such as in your post above)

You should use the .at() function instead. It is exception safe. The cplusplus.com reference says:

Strong guarantee: if an exception is thrown, there are no changes in the container.
It throws out_of_range if n is out of bounds.

Read: http://www.cplusplus.com/reference/vector/vector/operator[]/ http://www.cplusplus.com/reference/vector/vector/at/

Look at the bottom for exception safety.

AmN
  • 331
  • 1
  • 7