【发布时间】:2014-05-14 10:08:36
【问题描述】:
std::system_error 处理带有相关错误代码的异常。是否可以使用通用 catch 块来获取 std::system_error 异常消息及其代码?像这样
try{
// code generating exception
} catch (const std::exception& ex){ // catch all std::exception based exceptions
logger.log() << ex.what(); // get message and error code
// if exception type is system_error
}
唯一的方法是直接捕获std::system_error类型并在捕获基本异常类型之前获取其代码吗?广泛使用 std::system_error 的最佳方法是什么?
【问题讨论】:
-
嗯,也许您可以尝试使用
dynamic_cast检查,但您只需抓住std::system_error有什么困扰? -
@πάντα 没关系,如果存在更简洁的方式,我会很有趣。我不喜欢使用
dynamic_cast。 -
为什么不明确地捕捉
system_error?