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 to use C++ to send abnormal signals

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use C++ to send abnormal signals". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use C++ to send abnormal signals.

If the necessary task fails, use exception signaling Reason (reason)

It should not be possible to ignore an error because that could leave the system or a computation in an undefined (or unexpected) state. This is a major source of errors.

Errors may leave the system or calculation in an undefined (or unexpected) state, and the possibility of errors being ignored should be eliminated. Ignoring the handling of errors is the main cause of most problems.

Example (sample)

Int printf (const char*...); / / bad: return negative number if output fails

Template / / good: throw system_error if unable to start the new threadexplicit thread (frankly & f, Args&&... Args); Note (note)

What is an error?

What is a mistake?

An error means that the function fails to achieve its stated purpose (including the establishment of ex post facto conditions). Ignoring incorrect calling code may result in incorrect results or undefined system state. For example, the failure to connect to the remote server is not its own fault: the server may refuse to connect for a variety of reasons, so it is natural to return a result for the caller to confirm. If the inability to connect is considered an error, the failure should throw an exception.

Exception (exception)

Many traditional interface functions (e.g.UNIX signal handlers) use error codes (e.g.errno) to report what are really status codes, rather than errors. You don't have a good alternative to using such, so calling these does not violate the rule.

Many traditional functions (such as UNIX semaphore handlers) use error codes (such as errno) to report the actual status. Since there are no good options to replace them, calling these functions is not a violation of the rules.

Alternative (optional)

If you can't use exceptions (e.g., because your code is full of old-style raw-pointer use or because there are hard-real-time constraints), consider using a style that returns a pair of values:

If you cannot use exceptions (for example, because the code is full of old-style raw pointers, or because there are hard real-time constraints), consider using the style of return result pairs.

Int val;int error_code;tie (val, error_code) = do_something (); if (error_code) {/ /. Handle the error or exit...}

/ /... Useval...

Translator's note: tie is a new feature introduced by C++, which is used to unpack tuple data also introduced by Candles11.

This style unfortunately leads to uninitialized variables. Since clients 17 the "structured bindings" feature can be used to initialize variables directly from the return value:

One drawback of this approach is that it results in uninitialized variables. The structured binding feature that was imported after Category 17 was used can initialize variables directly by returning values.

Auto [val, error_code] = do_something (); if (error_code) {/ /. Handle the error or exit...} / /... Useval...

Note (Note)

We don't consider "performance" a valid reason not to use exceptions.

We don't think that "performance" is a reasonable reason not to use exceptions.

Often, explicit error checking and handling consume as much time and space as exception handling.

Usually, clear error checking and handling takes the same time and space as exception handling.

Often, cleaner code yields better performance with exceptions (simplifying the tracing of paths through the program and their optimization).

In general, using exceptions in clean code leads to better performance (simplifying tracer execution paths and optimizations)

A good rule for performance critical code is to move checking outside the critical part of the code (checking).

For performance-sensitive code, it is a good idea to move the inspection section away from key parts of the code.

In the longer term, more regular code gets better optimized.

Regular code can be better optimized in the long run.

Always carefully measure before making performance claims.

Be sure to consider carefully before declaring a performance problem. (translator's note: for example, will there really be performance problems? Is it really that important? )

Enforcement (implementation recommendations)

(Not enforceable) This is a philosophical guideline that is infeasible to check directly.

(non-mandatory) this is a principle related to programming philosophy and cannot be checked directly.

Look for errno.

Select errno.

Thank you for your reading. the above is the content of "how to use C++ to send abnormal signals". After the study of this article, I believe you have a deeper understanding of the problem of how to use C++ to send abnormal signals. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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