四、c++11错误处理

参考:http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-3.html

参考:http://zh.highscore.de/cpp/boost/errorhandling.html

1. std::error_code

value(): 当前平台的错误代码

category();

2. std::error_condition

value(): 与平台无关的错误代码,无视当前操作系统

category();

3. std::error_category

name():

message():

4. std::errc

作为std::error_condition常量的占位符。

可以通过std::errc的占位符来创建std::error_code或std::error_condition

5. std::error_code与std::errc可以直接进行“==”的比较
详情参考: http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-3.html

#include <iostream>
#include <system_error>
#include <thread>
int main()
{
    try {
        std::thread().detach(); // detaching a not-a-thread
    } catch (const std::system_error& e) {
        std::cout << "Caught a system_error\n";
        if(e.code() == std::errc::invalid_argument)
            std::cout << "The error condition is std::errc::invalid_argument\n";
        std::cout << "the error description is " << e.what() << '\n';
    }
}


将std::errc转化成error_condition还是error_code由注册决定

当std::errc注册为error_condition时:

template <> struct is_error_condition_enum<errc> : true_type {};

采用:bool operator==(const error_code& a,const error_condition& b);


当std::errc注册为error_code时: 

template <> struct is_error_code_enum<errc> : true_type {};

采用:

bool operator==(const error_code& a,const error_code& b);

5. make_error_code: 根据std::errc的值构建std::error_code

6. make_error_condition: 根据std::errc的值构建std::error_condition


详情参考: http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-3.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值