Careful use of exception specifications in C + +

Source: Internet
Author: User

There is no doubt that anomaly specifications are a compelling feature. It makes the code easier to understand because it clearly describes what kind of exception a function can throw. But it's not just an interesting note. The compiler can sometimes detect inconsistencies in the exception specification at compile time. And if a function throws an exception that is not in the exception specification range, the system can detect the error at run time, and then a special function unexpected will be invoked automatically. The exception specification can be both a guiding document and a mandatory constraint for exception use, and it seems to have a very attractive appearance.

But under normal circumstances, beauty is only a layer of skin, the beauty of appearance does not represent its intrinsic quality. function unexpected the default behavior is to call the function terminate, and terminate the default behavior is to call the function abort, so a program that violates the exception specification has the default behavior of halt (stop running). Local variables in the activated stack frame are not freed because abort does not perform such cleanup while closing the program. The violation of abnormal specifications became a disaster that should not have happened.

Unfortunately, it is easy for us to write the function that causes this disaster to occur. The compiler only partially detects that the use of exceptions is consistent with the exception specification. One function calls another function, and the latter may throw an exception that violates the exception specification of the former, (a function calls B function, because B function may throw an exception that is not within the specification of a function exception, so this function call violates the exception specification of a function) compiler does not detect this kind of situation, Also, language standards prohibit them from rejecting this invocation (although a warning message can be displayed).

For example, a function F1 does not declare an exception specification, such a function can throw any kind of exception:

extern void f1(); // 可以抛出任意的异常

Suppose a function F2 its exception specification to declare that it can only throw an int of the type:

void f2() throw(int);

F2 call F1 is very legitimate, even if F1 may throw an exception that violates the F2 exception specification:

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.