More than tive C ++ Reading Notes, Summary of clause 12

Source: Internet
Author: User

Clause 12: understand the differences between [throwing an exception] and [passing a parameter] or [calling a virtual function]

 

The clause states that the declaration Syntax of function parameters is exactly the same as that of catch clauses. Although [passing an exception to a catch clause from the throwing end] is basically the same as [passing an independent variable to a function parameter from the function call end], the difference is as follows:

Difference 1: Although function parameters and exceptions are transmitted in three ways: by value, by reference, by pointer. However, whether the parameter is passed or exception is completely different for the caller. When a function is called, the control will return to the calling end, but an exception is thrown, and the control will not return to the throwing end.

Difference 2: It is not allowed to pass a temporary object to a non-const reference parameter in a function call, but the conditions are legal.

Difference 3: Compared with [throwing an exception] and [passing function parameters], the former constructs a copy of the [thrown object] (and resolves it later)

 

 

The book pointed out that "no matter whether the caught exception is transmitted in the by value or by reference mode (it cannot be passed in the by pointer mode), replication of the exception object will occur, the copy that is delivered to the catch clause. However, when the thrown exception object is copied, the copy constructor of the called object is executed, and the copy constructor corresponds to the static type of the object rather than the dynamic type ."

[Exception objects is a copy of its object], which has an impact on how to spread exception in the Catch Block:

Catch (person & W) // catch person exception <br/>{< br/> ...... // Handle exception <br/> throw; // throw the exception again to enable it to continue propagation <br/>}</P> <p> catch (person & W) // catch person exception <br/>{< br/> ...... // Process exception <br/> throw; // a copy of The caught exception is propagated. <br/>}

The difference between the two catch blocks is that the former throws the current exception again, and the latter throws a copy of the current exception.

 

The difference between [independent variable transfer] and [Exception Propagation] is as follows:

1. When an object is moved from the [Caller or throw segment] to the [parameter or catch clause], the procedure is different.

2. the type matching rules are different between the [Caller or outbound segment] and [called or captured.

Third, the catch clause always tries to match in the order of occurrence. The first matched clause will handle this exception.

In the process of [Exception matching catch clause], there are only two conversions:

-- Inherit the class conversion (inheritance-based conversions) in the architecture. Refer to the exception inheritance system and the catch sub written for the base class exception.

Can handle exceptions of the derived class type. This rule applies to catch in three forms: by value, by reference, and by pointer.

-- But when converting from a [type pointer] to a [type-free pointer. For example, a catch clause designed for the const void * pointer can capture any pointer exception.

 

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.