哪个向量抛出了索引超出范围的异常
Which vector threw index out of range exception?
我想访问对抛出超出范围异常的std::vector
的引用,或者至少是抛出异常的行号(类似于Java的堆栈跟踪)。下面是一个示例程序:
#include <iostream>
#include <vector>
std::vector<int> vec1;
std::vector<int> vec2;
vec1.push_back(1);
vec2.push_back(2);
try
{
std::cout << vec1.at(1) << std::endl;
std::cout << vec2.at(1) << std::endl;
}
catch(Exception e)
{
// e.lineNumber()? e.creator_object()?
std::cout << "The following vector is out of range: " << ? << std::endl;
// or...
std::cout << "There was an error on the following line: " << ? << std::endl;
}
我知道这个例子是微不足道的,但我希望它展示了我正在寻找的功能。
编辑:Implementation, from g++——version: g++ (GCC) 4.1.2 20071124 (Red Hat 4.1.2-42)
你需要自己做:
#include <iostream>
#include <vector>
std::vector<int> vec1;
std::vector<int> vec2;
vec1.push_back(1);
vec2.push_back(2);
try
{
std::cout << vec1.at(1) << std::endl;
}
catch(std::exception& e)
{
std::cout << "The following vector is out of range: " << "vec1" << std::endl;
}
try
{
std::cout << vec2.at(1) << std::endl;
}
catch(std::exception& ex)
{
std::cout << "The following vector is out of range: " << "vec2" << std::endl;
}
相关文章:
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 为什么这个正则表达式C++在括号表达式中抛出无效范围异常?
- C++矢量超出范围异常
- 张量不会在超出范围索引时引发异常
- 如果一个对象是在本地创建的,并在C++中作为异常抛出,那么本地对象如何在其范围之外有效,即在 catch 块中?
- basic_string::替换的超出范围异常,而在范围内,正如调试相同参数的输出所证明的那样
- 当数字可能超出C++中特定数据类型的范围时如何处理异常?
- 程序崩溃,说向量不超出范围异常
- Windows 错误报告和超出范围的异常
- 对象在超出范围时是否被销毁,因为它们已引发异常.C++
- 从覆盖范围报告中删除自动生成的异常代码
- C++ 超出范围异常
- 为什么在 std::vector 中使用索引超出范围的运算符 [] 时没有出现异常?
- 为什么这会导致超出范围的异常
- 用于处理超出范围错误的异常类 - C++
- 当传递表示负值的十六进制时,C++stoll抛出超出范围的异常
- 字符串长度为10或更长的c++stol抛出范围外异常
- C++:如果抛出异常,那么超出范围的对象将被销毁
- 给定范围内的完美平方:循环执行异常
- 通过引用捕获(使用 catch)时异常对象的范围