C++ Do I need to write throw clause for a function everywhere?

我怕爱的太早我们不能终老 提交于 2019-12-05 15:57:13

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

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.

Good Read:
A Pragmatic Look at Exception Specifications

Declaring exceptions is a bad idea. From http://www.gotw.ca/publications/mill22.htm :

Moral #1: Never write an exception specification.
Moral #2: Except possibly an empty one, but if I were you I’d avoid even that.

Exception specifications are deprecated in C++11 - do not write exception specifications unless it is to specify that your function is guaranteed not to throw an exception.

Note that actually providing this guarantee is harder than it may seem - Herb Sutter has written many articles and a book on the subject of exceptions and exception safety.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!