标签:inf 存在 handler type 开始 运行时错误 删除对象 exce 遇到的问题
for(declaration:expression) statement;vector<int>v = {0,1,2,3,4,5,6};
for(auto &r : v) {
     r *= 2;
}
//等价于
for(auto beg = v.begin(),end = v.end(); beg != end; beg++) {
    auto &r = *beg;
    r *= 2;
}
在C++中,异常处理包括
Sale_item item1,item2;
cin >> item1 >> item2;
if(item1.S() == item2.S()) {
    cout << item1.S() + item2.S() << endl;
    return 0;
}else {
    cout << "error" << endl;
    return -1;
}
//等价于
if(item1.S() != item2.S()) {
    throw runtime_error("error");
}
cout << item1 + item2 << endl;
//通用语法形式
try {
    program-statements;
} catch (exception-declaration) {
    handler-statements;
} catch (exception-declaration) {
    handle-statements;
}
如上面的例子,我们可以使用
while(cin >> item1 >> item2) {
    try {
        cout << item1.S() + item2.S() << endl;
        break;
    } catch (runtime_error err) {
        cout << "err.what(), try again?" << endl;
    }
}
C++标准库定义了一组类用于报告标准库函数遇到的问题,这些异常类也可以在用户编写的程序中使用,他们分别定义在四个头文件中
stdexcept定义的异常类
异常类型只定义了一个名为what()的成员函数,没有任何参数,返回一个const char*,该字符串用于提供关于异常的文本信息
标签:inf 存在 handler type 开始 运行时错误 删除对象 exce 遇到的问题
原文地址:https://www.cnblogs.com/Hugh-Locke/p/13221580.html