"C + +" exception Brief (iii): How to look at C + + exceptions

Source: Internet
Author: User

The use of C + + exception, I believe in the above summary has been more complete, this article mainly on the C + + exception This section of the additional supplement.

Even though C + + has been adding exceptions to the standard for many years, it is now possible to see a lot of persistent exceptions that are not explicitly used . (including myself, seldom use exceptions in the code written).

Before commenting on the exception, first introduced the Google C + + coding specification, we first to see how Google sees C + + exceptions.

6. Exceptions (Exceptions)

Do not use C + + exceptions.

Advantages:

1) exceptions allow upper-layer applications to decide how to handle the "impossible" failures that occur in the underlying nested functions, not as obscure as the record of the error code;
2) In many other modern languages, the introduction of exceptions makes C + + more compatible with Python, Java, and other languages that are similar to C + +;
3) Many C + + third-party libraries use exceptions, and closing exceptions will lead to difficulty in combining them;
4) Exceptions are the only solution to the failure of the constructor, although they can be simulated by the factory function (factory functions) or the Init () method, but they require heap allocations or new "illegal" states, respectively;
5) in the test framework (Testing framework), the exception is indeed very useful.

Disadvantages:

1) When you add a throw statement to an existing function, you must check all calls, even if they have at least basic exception security, or the program ends normally, it is never possible to catch the exception.
For example, if f () calls G () and H () in turn, H throws an exception caught by F, the G should be careful to avoid not being completely cleaned up;
2) in layman's terms, exceptions cause program control flow to be determined by viewing the code: The function may return in an indeterminate place, leading to difficulty in code management and debugging, and of course, you can minimize the overhead by stipulating how and when to use the exception. Give developers the burden of mastering these rules;
3) Abnormal security requires RAII and different coding practices. It takes a lot of support to easily and correctly write exception-safe code. Allow the use of exceptions;
4) Adding an exception makes the binary execution code larger, increases the compile time (perhaps less), and may increase the address space pressure;
5) The practicality of the anomaly may stimulate the developer to throw an exception at an inappropriate time, or recover from an exception in an unsafe place,
For example, illegal user input can cause an exception to be thrown. If you allow the use of exceptions, this will make a lot of programming style guide (the translator note, this reason a bit farfetched:-()!

Conclusion:

On the surface, the use of exception benefits outweigh the disadvantages, especially in new projects, however, for existing code, introducing exceptions will implicate all dependent code.
If exceptions are allowed to be used in new projects, it is also a hassle to integrate with code that has not previously used exceptions.
Because most of Google's existing C + + code has no exception handling, it is quite difficult to introduce new code with exception handling.
Given that Google's existing code does not accept exceptions, using exceptions in existing code is a little more expensive than using them in new projects, and the migration process can be slow and error prone. We also do not believe that an effective alternative to exceptions, such as error codes, assertions, etc., is a serious burden.
We do not oppose the use of anomalies on the philosophical or moral level, but on the basis of practice. Because we want to use open source projects on Google, the use of exceptions in our projects can be inconvenient because we also recommend not to use exceptions in open source projects on Google, which is obviously unrealistic if we need to reinvent them.
For Windows code, there is an exception (wait until the last article: D).
Note: For exception handling, obviously not just a few words can be said clearly, take the constructor as an example, many C + + books are mentioned when the construction fails only exception can be handled, Google prohibit the use of exceptions this point, just for their own convenience, said big, but is based on software management costs, In practice, it's up to you to decide.

Here are a few more discussions on C + + exceptions: https://www.zhihu.com/question/22889420

"C + +" exception Brief (iii): How to look at C + + exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.