访问内存中不存在的数组索引的正确异常类型?

Right type of exception for access of array index which doesn't exist in memory?

我是 C++ 异常的新手,我无法为错误的内存访问尝试找到正确的异常类型。

假设我使用以下代码为 10 个整数分配了内存:

int* intArray = new int[10];

for(int i = 0;i < 10;i++){
    intArray[i] = i+1;
}

在尝试阅读它时我犯了一些逻辑错误,这将导致程序访问我没有分配的内存,例如:

for(int i = 0;i < 15;i++){
    cout << intArray[i] << endl;
}

要捕获的正确异常类型是什么?

谢谢!

这个代码

int* intArray = new int[10];
...    
for(int i = 0;i < 15;i++){
    cout << intArray[i] << endl;
}

没有抛出异常。这将导致未定义的行为。最有可能的是,您只是得到随机数据的输出。当您使用 new 运算符创建数组时,您会得到一系列具有连续地址的元素。 C++ 没有在运行时检查此类数组长度的机制。