C++核心准则E.14:使用根据目的设计的用户定制类型异常(非内置类型)
发布日期:2021-07-01 05:29:19 浏览次数:2 分类:技术文章

本文共 2181 字,大约阅读时间需要 7 分钟。

E.14: Use purpose-designed user-defined types as exceptions (not built-in types)

E.14:使用根据目的设计的用户定制类型异常(非内置类型)

 

Reason(原因)

A user-defined type is unlikely to clash with other people's exceptions.

用户定义类型不大可能和其他人的异常发生冲突。

 

Example(示例)

void my_code(){    // ...    throw Moonphase_error{};    // ...}void your_code(){    try {        // ...        my_code();        // ...    }    catch(const Bufferpool_exhausted&) {        // ...    }}

Example, don't(反面示例)

void my_code()     // Don't{    // ...    throw 7;       // 7 means "moon in the 4th quarter"    // ...}void your_code()   // Don't{    try {        // ...        my_code();        // ...    }    catch(int i) {  // i == 7 means "input buffer too small"        // ...    }}

 

Note(注意)

The standard-library classes derived from exception should be used only as base classes or for exceptions that require only "generic" handling. Like built-in types, their use could clash with other people's use of them.

继承自exception的标准库类应该只用于基类或只要求“通常”处理的异常。和内置类型相似,你对它们的使用可能和其他人的使用发生冲突。

 

Example, don't(反面示例)

void my_code()   // Don't{    // ...    throw runtime_error{"moon in the 4th quarter"};    // ...}void your_code()   // Don't{    try {        // ...        my_code();        // ...    }    catch(const runtime_error&) {   // runtime_error means "input buffer too small"        // ...    }}

See also: Discussion

参见:话题讨论

 

Enforcement(实施建议)

Catch throw and catch of a built-in type. Maybe warn about throw and catch using a standard-library exception type. Obviously, exceptions derived from the std::exception hierarchy are fine.

捕捉针对内置类型的throw和catch。也许可以针对使用标准库异常类型的throw和catch发出警告。显然,继承自std::exception的异常类没有问题。

 

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#e14-use-purpose-designed-user-defined-types-as-exceptions-not-built-in-types

 

新书介绍

以下是本人3月份出版的新书,拜托多多关注!

 

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

 


 

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

 

转载地址:https://oothinking.blog.csdn.net/article/details/108828716 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++核心准则E.15: 使用引用形式捕捉继承体系中的异常
下一篇:C++核心准则E.13: 直接拥有一个对象所有权时永远不要抛出异常

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月28日 10时16分45秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

oracle 2021-07-11
rpm no signed 2021-07-11
oracle 基本sql语句 2021-07-11
问题: 2021-07-11
linux 下安装那个oracle客户端版本的 在哪里配置tnsnames呢 2021-07-11
linux/window mysql 操作 和 问题 2021-07-11
temp code 2021-07-11
协议指令意义以及代码 2021-07-11
编译删除 2021-07-11
gdb 命令 2021-07-11
硬件申请 2021-07-11
iscsi 2021-07-11
reador哦 2021-07-11
source sight 显示 utf-8的中文 2021-07-11
关机!!! 2021-07-11
beep 2021-07-11
远程控制 2021-07-11
vs2008快捷键极其技巧 2021-07-11
total commande 2021-07-11
oracle 2021-07-11