Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to ensure that the destructor does not throw an exception in C++

Shulou Source: shulou.com Published: 2022-06-02 06:08:23 10月19日 Update

C++ how to ensure that destructors do not throw exceptions, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

A destructor may not fail. If a destructor tries to exit with an exception, it's a bad design error and the program had better terminate.

All destructors do not fail. If the destructor tries to throw an exception to exit, this is a serious design error, and a better option is to abort the program.

Note (Note)

A destructor (either user-defined or compiler-generated) is implicitly declared noexcept (independently of what code is in its body) if all of the members of its class have noexcept destructors. By explicitly marking destructors noexcept, an author guards against the destructor becoming implicitly noexcept (false) through the addition or modification of a class member.

If the destructors of all members of a class are noexcept, its destructors (whether user-defined or compiler-generated) are implicitly defined as noexcept (regardless of the specific code in the function body). By explicitly defining the destructor as noexcept, you can prevent the destructor from becoming noexcpet because the class member has been modified.

Example (sample)

Not all destructors are noexcept by default; one throwing member poisons the whole class hierarchy

Not all destructors are noexcept by default; as long as one member throws an exception, it destroys the entire inheritance system.

Struct X {

Details x; / / happens to have a throwing destructor

/ /...

~ X () {} / / implicitly noexcept (false); aka can throw

}

Swipe left and right to see more

So, if in doubt, declare a destructor noexcept.

Therefore, if in doubt, define the destructor as noexcept.

Note (Note)

Why not then declare all destructors noexcept? Because that would in many cases-especially simple cases-be distracting clutter.

Why not define all destructors as noexcept? Because in many situations, especially in simple situations, doing so will only increase the interference of information.

Enforcement (implementation recommendations)

(Simple) A destructor should be declarednoexceptif it could throw.

(simple) if there is a risk of throwing an exception, define the destructor as noexcept.

After reading the above, do you know how to ensure that the destructor does not throw an exception in C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Function member more guaranteed content situation method problem helpless to this end code system information reason to this suggestion skill user question Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info MySQL vpn Microsoft Docker