More effective C + + clause 12 Understanding the difference between "throwing a exception" and "passing a parameter" or "calling a virtual function"

Source: Internet
Author: User
Tags throw exception

1. The return value of the function is quite similar to the try block throw exception, the function receive parameter and the catch sentence catch exception (not only the declaration form, the function parameter and the exception pass way have three kinds: by Value,by reference,).

2. Although a function call is quite similar to an exception throw, "Passing a exception from the throw side to the catch clause" and "passing an argument from the function call to the tuned function parameter" still varies greatly:

1) Call a function, control will eventually return to the end of the call (unless the function fails to return), but throw a exception, control will not return to the throw end

It is simple to understand that the function call scope is the "outside-inside-outside" conversion, and the exception is thrown "inside-out-" Conversion (just for the sake of understanding, actually this analogy is not correct)

2) If the arguments of a function call are passed by reference, the arguments are not copied, but the thrown exception object is copied at least once, regardless of whether the catch receives an exception by reference or by value, because the local objects are destroyed during the stack expansion. Thus it is necessary to produce a temporary object to hold the thrown exception, which is the same as the object that temporarily saves the return with a temporary object when the function return (the function return exists NRV (some also called Rvo) optimization, can omit the call copy constructor). That is, when the first catch clause accepts an exception, that exception is already a temporary object that was copied once, and if the parameter of the catch clause is passed by value, the temporary object needs to be replicated again. Therefore, exception handling usually pays a high price.

3) A function can return a reference, and a catch clause may not be able to re-throw a reference, for the following code:

1 Try {2     Throw Derived; 3 }4catch(Base & tmp) {5     throw  tmp; 6 }
View Code

The catch clause is re-thrown during the process of creating a temporary object and invoking the copy constructor, because the constructor cannot be virtual (although it can take other forms to form a virtual "pseudo-constructor"), which means that if the exception that you throw through the catch clause has become a base type ( Although passed in by reference, the exception is a copy of the current exception. If you want to re-throw the derived type object, you can use the following code:

1 Try {2     Throw Derived 3 }4catch(based& tmp) {5     throw; 6 }
View Code

This throws the current exception.

4) function calls and exception throw parameter matching rules, if there are more than one overloaded function, then the selection of parameters most match the one, can not find a matching function is the conversion of the actual argument to match, there are a number of fairly matched functions are two semantic, that is, function matching using "best fit" strategy;

Exception throws are different, catch clauses in the order of occurrence matching attempt, once found a "relatively compatible" type is considered a successful match, even if the following exact match of the type will be ignored, that is, the exception thrown by the parameter matching using "first match" strategy, and because of this strategy, Exception-thrown arguments allow conversions that are much more restrictive than those allowed by function argument matching, allowing only the following conversions:

1) "Class conversion in inheritance Schema": derived class exceptions can be captured by base class parameters, so the order in which the catch clauses appear should be derived classes first class

2) Conversion of non-const to const

3) array pointer to array type

4) Other pointer to "non-type pointer" (void* pointer)

More effective C + + clause 12 Understanding the difference between "throwing a exception" and "passing a parameter" or "calling a virtual function"

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.