c++ out_of_range
来源:互联网 发布:动物知乎 编辑:程序博客网 时间:2024/05/19 19:15
转载:http://blog.csdn.net/wxqian25/article/details/14230523
对std::out_of_range抛出异常进行处理 ,头文件stdexcept
- #include <iostream>
- #include <vector>
- #include <stdexcept>
- using namespace std;
- int main() {
- vector <int> a;
- a.push_back(1);
- try {
- a.at(1);
- }
- catch (std::out_of_range &exc) {
- std::cerr << exc.what() << " Line:" << __LINE__ << " File:" << __FILE__ << endl;
- }
- return EXIT_SUCCESS;
- }
这样就能知道在第几行和哪个文件中了。
说明编辑
C++异常类,继承自logic_error,logic_error的父类是exception。属于运行时错误,如果使用了一个超出有效范围的值,就会抛出此异常。也就是一般常说的越界访问。定义在命名空间std中。
使用时须包含头文件 #include<stdexcept>
out_of_range例子
编辑// out_of_range example
#include<iostream>
#include<stdexcept>
#include<vector>
using namespace std;//或者用其他方式包含using std::logic_error;和using std::out_of_range;
int main (void)
{
vector<int> myvector(10);
try
{
myvector.at(20)=100; // vector::at throws an out-of-range
}
catch (out_of_range& oor)
{
cerr << "Out of Range error: " << oor.what() << endl;
}
getchar();
return 0;
}
myvector只有10个元素,所以myvector.at(20)就会抛出out_of_range异常。
0 0
- c++ out_of_range
- std::out_of_range错误
- out_of_range 标准异常
- std::out_of_range 抛出抛出异常
- 对std::out_of_range抛出异常进行处理
- Microsoft C++ exception: std::out_of_range at memory location 0x03def46c..
- terminate called after throwing an instance of 'std::out_of_range'
- 运行时错误: terminate called after throwing an instance of 'std::out_of_range'
- terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr
- terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check
- 调bug:terminating with uncaught exception of type std::out_of_range: vector Abort trap: 6
- Unhandled exception at 0x00007FFE7BFD8A5C in wb.exe: Microsoft C++ exception: std::out_of_range at m
- Unhandled exception at 0x00007FFE7BFD8A5C in wb.exe: Microsoft C++ exception: std::out_of_range at m
- c
- c
- c
- c
- C
- LeetCode 343. Integer Break
- matlab 常用 api(六)
- 关于Webview带参访问服务器
- 微信js-sdk config JavaScript签名
- 虚函数和函数重载
- c++ out_of_range
- FIR滤波器以及吉布斯效应
- 类扩展与分类
- 在iphone越狱机器中使用Hook
- 沙盒
- JSON 与JSONP
- 100. Same Tree
- curl和wget
- iOS跳转到AppStore,AppStore检查版本更新(上线会被拒),跳转到设置界面