I was trying to produce an error for a try-catch testing with this code, where I expected to get an error when accessing a[3]
(fourth) element. Even when not getting the error, the for loop must stop after five iterations, which never occurs.
int a[3] = {1, 2, 3};
for(int i = 0; i < 5; i++)
{
std::cout << i << ": " << a[i] << std::endl;
}
output:
0: 1
1: 2
2: 3
3: 1970756548
4: 4201552
5: 2686800
6: 2130567168
7: 0
8: 0
9: 2686824
10: 4198992
.
.
.
4150: 0
4151: 0
4152: 0
4153: 0
4154: 0
It's undefined behavior(UB), you don't have (at least) five elements in your array. You cannot catch UB, exceptions is what we catch
. Nothing more to debate here, but see the interesting link in the comments section.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With