C++怎么定制类型异常

本篇内容主要讲解“C++怎么定制类型异常”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++怎么定制类型异常”吧!

创新互联公司是专业的锦州网站建设公司,锦州接单;提供网站设计、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行锦州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

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"
       // ...
   }
}

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的异常类没有问题。

到此,相信大家对“C++怎么定制类型异常”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


分享文章:C++怎么定制类型异常
浏览路径:http://bzwzjz.com/article/pdpihh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设 营销型网站建设 网站建设开发 成都网站设计制作公司 成都网站制作公司 成都网站设计公司 营销型网站建设 成都企业网站建设公司 网站建设改版 网站设计 成都网站建设 四川成都网站建设 企业手机网站建设 成都企业网站制作 成都网站建设 成都网站建设 网站制作 网站制作 成都网站设计 营销型网站建设 成都营销网站制作 营销网站建设