0

I am trying to learn exception throwing on C++ My code is basically:

int nums[3] = { 1, 5, 57 };
try {
    cout << nums[4] << endl;
}
catch (logic_error e) {
    cout << e.what() << endl;
}

I thought that here I would get an out_of_range exception and I could catch it, but the program runs normally, and I get this as a result:

-277035873

It's always a different negative number. Can somebody explain to me what's happening? I couldn't find an answer online to this specific case.

I am using Visual Studio 2017 Version 15.9.34

Thanks.

Andrew.M
  • 90
  • 1
  • 7
  • Have you seen the following post? https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why – David Tansey Mar 27 '21 at 20:23
  • Use a [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) and [`at`](https://en.cppreference.com/w/cpp/container/vector/at) for bounds checking. – dxiv Mar 27 '21 at 21:05

0 Answers0