前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则E.31:正确排列catch子句

C++核心准则E.31:正确排列catch子句

作者头像
面向对象思考
发布2020-08-13 11:29:17
3070
发布2020-08-13 11:29:17
举报
文章被收录于专栏:C++核心准则原文翻译

E.31: Properly order your catch-clauses

E.31:正确排列catch子句

Reason(原因)

catch-clauses are evaluated in the order they appear and one clause can hide another.

catch子句按照它们表示的次序行,一个子句出发之后,其他子句不再执行。

Example(示例)

代码语言:javascript
复制
void f()
{
    // ...
    try {
            // ...
    }
    catch (Base& b) { /* ... */ }
    catch (Derived& d) { /* ... */ }
    catch (...) { /* ... */ }
    catch (std::exception& e) { /* ... */ }
}

If Derivedis derived from Base the Derived-handler will never be invoked. The "catch everything" handler ensured that the std::exception-handler will never be invoked.

如果Deriveds是Base的派生类,捕捉派生类的处理永远不会执行。捕捉所有异常的处理会导致捕捉std::exception的处理程序永远不会执行。

Enforcement(实施建议)

Flag all "hiding handlers".

标记所有被隐藏的异常处理程序。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e31-properly-order-your-catch-clauses

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档