More effective C + + ITEM14: Using exception wisely specifications

Source: Internet
Author: User

With exception specifications you have to be very careful to make sure that the function call's child function, the registered callback function, does not violate the Convention. It is more difficult to ensure that exceptions are inside the design template.

When designing a callback mechanism, if the caller prescribes a non-throwing exception, you must ensure that the registered function does not throw an exception, as shown in the book:

void (*callbackptr) (    int  eventxlocation,    int  eventylocation,     void *datatopassback    throw();

and registers the function with the callbackptr type. SM greatly mentions that the standard does not allow exception specifications within a TypeDef, which is a tacit practice for many compilers.

VC + + approach is this: VC + + has such a extended attribute syntax called __declspec, declaring the function with __declspec (nothrow) can have and throw () the same effect. The example given on MSDN https://msdn.microsoft.com/en-us/library/49147z04.aspx:

#define WINAPI __declspec (nothrow) __stdcall

The paper also points out that it is convenient to declare functions extensively.

The compiler is not responsible for exception specifications! Give a warning, and the rest is the run-time thing.

When the runtime throws an exception that is not in the exception specifications, the unexpected function is called, and one of the recommended practices in clause 14 is to use set_unexpected redefine unexpected to throw a custom "unknown exception" And join this thing in all the exception specifications. But the set_unexpected of the pro-test VS2015 will not behave correctly.

Another way is to write the new unexpected function as a direct throw, go out, this will instead throw std::bad_exception, and then add this to exception specifications. And so the high-level may have done a lot of preparation for the exception will be the bottom of the "kindness" and waste, which requires the unification of engineering.

More effective C + + ITEM14: Using exception wisely specifications

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.