C ++我需要写一个函数的throw子句吗? [英] C++ Do I need to write throw clause for a function everywhere?

查看:126
本文介绍了C ++我需要写一个函数的throw子句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑有一个类和一个全局函数:

Consider to have a class and a global function:

usefulfuncts.hpp

void dosome(int a, int b) throw (std::exception);

这是 usefulfuncts.cpp

void dosome(int a, int b) throw (std::exception) {
   //...
}

这是aclass.hpp

And this is aclass.hpp

class aclass {
   // Members...
   friend void dosome(int a, int b) throw (std::exception);
   // Members...
};



之后(我想要的)



好的!我想知道是否严格必须写每次 throw 子句。例如,我可以这样做吗?

After (what I would like that to be)

Ok! I would like to understand if it is strictly necessary to write everytime the throw clause. So for example can I do this?

这是 usefulfuncts.hpp

void dosome(int a, int b) throw (std::exception);

这是 usefulfuncts.cpp

void dosome(int a, int b) { /* OMITTING IT! */
   //...
}

hpp

class aclass {
   // Members...
   friend void dosome(int a, int b); /* OMITTING IT */
   // Members...
};

这是对的吗?只把它放在主声明中?感谢

Is this right? To put it only in the main declaration? Thanks

推荐答案

省略异常规范意味着您的函数可以引发 任何 exception。

Omitting a exception specification means that your function can throw any exception.

异常规格不良。几乎没有任何编译器正确实现该功能。自从C ++ 11标准以来,它们已被弃用。事实上,异常规范被认为是失败的实验,即使他们是C ++ 03标准的一部分。

Exceptions specifications are bad. There are hardly any compilers which implement the feature correctly. They have been deprecated since the C++11 Standard. In fact Exception specifications were considered a failed experiment even while they were a part of the C++03 standard.

良好阅读:

异常规范例外规范

这篇关于C ++我需要写一个函数的throw子句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆