Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C++ use noexcept for functions that throw exceptions

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how C++ uses noexcept for functions that throw exceptions. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "C++ how to use noexcept for functions that throw exceptions"!

If the function does not throw an exception, declare noexceptReason

If an exception is inferred not to be thrown, the program does not have to assume that it needs to handle errors and terminate execution as soon as possible. Declaring a function as noexcept helps optimize your program by reducing the number of possible execution paths. Doing so will also speed up exit processing after failure.

Example

Add the noexcept modifier to functions written entirely in C or in other languages where exceptions do not occur. The C++ standard library implicitly does the same thing for all C standard library functions.

Note:

The constexpr function can throw exceptions at runtime, so you can specify noexcept for some (but not all) constexpr functions.

Translator's Note: Divided by 0, for example.

Example

You can even use noexcept for functions that throw exceptions.

vector collect(istream& is) noexcept{ vector res; for (string s; is >> s;) res.push_back(s); return res;}

If collect() runs out of memory, the program crashes. Unless the program is carefully designed to avoid running out of memory, this may be the only thing you can do. terminate() may generate appropriate error log messages (but it's hard to do anything delicately when memory runs out)

Declaring noexcept tells the compiler that it doesn't need to generate traps and pass-out exceptions. If an exception does occur, the result is as described in this section. What the author is trying to say here is that misspecifying the noexcept attribute is risky.

Note:

When deciding whether to label a function noexcept, you must pay attention to the execution environment in which the code is executed. The most important reason is the issue of throwing exceptions and memory allocation. Code that attempts to be perfectly universally used (e.g., standard libraries or similar code) needs to support environments where bad_loc exceptions can be meaningfully handled (rather than simply aborting execution). However, most programs and execution environments cannot meaningfully handle memory allocation failures, and aborting programs in those cases is the cleanest and easiest way to handle memory request failures. If you know that your application cannot handle memory allocation errors, it may be appropriate to add noexcept to functions that include memory allocation actions. This triggers termination processing.

In other words, most programs, most functions throw exceptions (for example, they may use new, they may call functions that throw exceptions, or they may use library functions that report errors by throwing exceptions), so don't use noexcept everywhere without thinking about whether the exception will be handled.

Noexcept is especially useful for low-level functions that are frequently used (and easy to judge for correctness).

Note:

Destructors, swap functions, move operations, and default constructors should never throw exceptions.

Enforcement

Flag functions that are not noexcept, yet cannot throw.

Mark functions that do not have the noexcpet attribute but do not throw exceptions.

Flag throwing swap, move, destructors, and default constructors.

Tag swap/move operations that throw exceptions, destructors, and default constructors.

At this point, I believe that everyone has a deeper understanding of "how C++ uses noexcept for functions that throw exceptions," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report