小编cli*_*cli的帖子

C ++函数未捕获向量下标超出范围的执行

我试图在字符“:”上拆分一个字符串,然后查看索引 2 是否存在。我的分割字符串函数工作得很好,因为我已经使用它很长一段时间了,但是在尝试尝试{} catch()之后,它没有捕获执行,而是在我的屏幕上显示调试错误。

    std::vector<std::string> index = split_string(random_stringgg);

    std::cout << index[1] << std::endl;

    try {
        std::string test = index[2];   
    }
    catch (...) {
        std::cout << "error occured" << std::endl;
        return false;
    }
    std::cout << "no error";
Run Code Online (Sandbox Code Playgroud)

据我所知,这应该尝试找到向量“索引”的第二个索引,然后捕获执行(如果没有/无法找到它)。然而,这对我不起作用,即使在添加 try/catch 之后,它也会抛出“向量下标超出范围”。我的问题是为什么它没有捕获并仍然显示错误?

c++ exception outofrangeexception c++14

1
推荐指数
1
解决办法
2282
查看次数

标签 统计

c++ ×1

c++14 ×1

exception ×1

outofrangeexception ×1