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 solve it if C++ can't throw an exception?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "C++ can not throw an exception how to solve", in the daily operation, I believe that many people have doubts about how to solve the problem that C++ can not throw an exception. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt that "C++ can't throw an exception". Next, please follow the editor to study!

E.25: if you cannot throw an exception, imitate RAII for resource management

Reason (reason)

Even if not used with exceptions, RAII is usually the best and most systematic way to handle resources.

Note (Note)

Error handling with exceptions is the only way to completely and systematically handle non-local errors in C++. In general, non-intrusive error signals are needed to construct an object. Sending an error signal in a manner that cannot be ignored requires an exception. If you can't use an exception, simulate it as much as you can.

A lot of fears about anomalies are misguided. When using exceptions in a code environment that is not messed up by pointers or complex control structures, exception handling is almost always acceptable (whether in time or space dimensions) and almost always leads to better code. Of course, imagine a good implementation of an exception handling mechanism that does not exist in any system. There are still situations that do not belong to the above problem, but cannot use exceptions for other reasons. Some hard real-time systems are one example: an operation must be completed within a fixed period of time and get correct or wrong results. If there is no appropriate time evaluation tool, it is very difficult to meet this requirement. Such systems, such as flight control systems, usually also prohibit the use of dynamic (heap) memory.

Therefore, the main guideline for error handling is "use exceptions and RAII". This section deals with situations where either you can't get an exceptionally efficient implementation, or your code is old code like a mouse nest (for example, a large number of pointers, ownership of morbid definitions, a large number of unsystematic error handling based on test errors), simple and systematic exception handling cannot be introduced. Before condemning the exception or complaining about the high cost of the exception, consider the cost and complexity of using error code. If you are worried about performance, measure it (not unfounded doubts).

Example (sample)

Suppose you want to write the following code:

Void func (zstring arg)

{

Gadget g {arg}

/ /...

}

If gadget cannot be built correctly, func exits because of an exception. If you can't throw an exception, we can simulate RAII-style resource management by adding a valid member function to Gadget.

Error_indicator func (zstring arg)

{

Gadget g {arg}

If (! g.valid ()) return gadget_construction_error

/ /...

Return 0; / / zero indicates "good"

}

The problem here, of course, is that the caller must remember to check the return value.

Enforcement (implementation recommendations)

It is possible (only) to apply a specific version of the idea: for example, to systematically check valid () after the resource handle is built.

At this point, the study of "C++ can not throw an exception how to solve" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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