The exception thrown in the member function of the C ++ object

Source: Internet
Author: User

C ++ Exception Handling model not only supports process-oriented C styleProgramIn addition to Exception Handling (that is, there is no object-oriented concept, it is completely a C program. The whole program is actually a collection of functions, but it uses the C ++ compiler to compile such a C program, therefore, such a program can use the Exception Handling Mechanism of C ++. How can c ++ be compatible with C? However, you must note that C ++ Exception Handling models cannot be used for programming in pure C language programs. Is it a bit difficult to say? A little confused? In fact, it is very simple, that is, if the program uses the C ++ exception handling mechanism, that isCodeIf the keyword try, catch, and throw exists, you must use the C ++ compiler to compile the program. Many programmers and friends have a misunderstanding here. They think that only when the program uses the object-oriented concept, that is, using the class keyword to define a class structure is equivalent to the C ++ program, in fact, this understanding is one-sided. If the C ++ exception handling mechanism is adopted in the program, there are also reasons to think that this is a C ++ program, even if the program code is completely in the C language style and such a program is compiled using the C compiler, an error will be reported, prompting undefined try identifiers and other error messages ), it also supports Exception Handling thrown by objects in object-oriented programs.

The C ++ Exception Handling Model is indeed closely integrated with the object-oriented model. In addition to describing exceptions in a program using objects described in the encounter article, the C ++ Exception Handling model also provides the most complete support and processing for exceptions thrown by object instances in object-oriented programs. Maybe everyone will think this is easy and there is nothing remarkable about it. But on the contrary, this is actually the most successful, incredible, and shining part of the C ++ Exception Handling Model. In addition, the C ++ Exception Handling model has good support and compatibility with object-oriented systems, which makes the implementation of the C ++ Exception Handling Model extremely complex, because it needs to track the running status and status of each object (the implementation of the C ++ Exception Handling Model will be detailed in the secrets of love ). This article and the following articlesArticleThis section describes how to handle an exception thrown by an object instance.

An object has three statuses: Construction, running, and destruction. Therefore, there are three differences between the exceptions thrown by objects. Is it thrown during Object Construction? Or is it thrown when the object is running? Or is it thrown when the Destructor object is created? These three exceptions will produce different results. This article first discusses the most common situation, the exception thrown during object running, that is, the exception encountered when executing the object's member function.

The member function of the object throws an exception.

1. The old method is as follows:

Class mytest_base

{

Public:

Mytest_base (string name = ""): m_name (name)

{

Cout <"construct an object of the mytest_base type with the object name:" <

}

Virtual ~ Mytest_base ()

{

Cout <"destroy an object of the mytest_base type with the object name:" <

}

Void func () Throw ()

{

Throw STD: exception ("intentionally throw an exception, test !");

}

Void other (){}

Protected:

String m_name;

};

Void main ()

{

Try

{

Mytest_base obj1 ("obj1 ");

// An exception will be thrown when this member function is called. Do you note that the destructor of obj1 will be executed? If

// Yes. When will it be executed?

Obj1.func ();

Obj1.other ();

}

Catch (STD: exception E)

{

Cout <E. What () <Endl;

}

Catch (...)

{

Cout <"unknow exception" <Endl;

}

}

C ++ programmers can easily see the running results of the above program, as shown below:

Construct a mytest_base object named obj1

Destroys a mytest_base object named obj1.

Intentionally throw an exception and test it!

The following conclusions can be drawn from the running results:

(1) When an exception occurs to a member function of an object, the catch block can catch the exception, just like a common function in C language;

(2) When an exception occurs to the object's member functions, the object's destructor will be executed (this is amazing! Of course, I will not do much research here. I will elaborate on the implementation of the C ++ Exception Handling Model in detail ), this is consistent with the object-oriented features specified in the C ++ standard. The constructed object must be analyzed in an appropriate place to release possible resources. Therefore, the "c ++ Exception Handling Model supports and is compatible with object-oriented" is justified. Note that the Destructor is executed before the exception handling module, which is more consistent with the object-oriented features stipulated in the C ++ standard, when an object has a scope, it must be destructed.

2. Modify the above program and run it again. The result is as follows:

Void main ()

{

// The obj1 object is not in the trycatch domain. When will its destructor be executed?

Mytest_base obj1 ("obj1 ");

Try

{

// Both the obj2 and obj3 objects are in the trycatch domain, where the obj3.func () function is called, so

// Obj3 throws an exception. Note that the destructor of obj2 will be executed? If

// Yes. When will it be executed?

Mytest_base obj2 ("obj2"), obj3 ("obj3 ");

Obj3.other ();

// An exception will be thrown when this member function is called.

Obj3.func ();

// Note: An exception is thrown in the function before the obj4 object is constructed. So the obj4 object will not

// Constructed, and of course not destructed

Mytest_base obj4 ("obj4 ");

Obj3.other ();

}

Catch (STD: exception E)

{

Cout <E. What () <Endl;

}

Catch (...)

{

Cout <"unknow exception" <Endl;

}

}

The above program is also difficult to see its running results, as shown below:

Construct a mytest_base object named obj1

Construct a mytest_base object named obj2

Construct a mytest_base object named obj3

Destroys a mytest_base object named obj3.

Destroys a mytest_base object named obj2.

Intentionally throw an exception and test it!

Destroys a mytest_base object named obj1.

Based on the problems raised in the program and the running results, the following conclusions can be drawn:

(1) When an exception occurs in a member function, objects that have not yet been constructed after an exception occurrence point in the same scope will not be constructed, and of course will not be destructed;

(2) When an exception occurs in a member function, the constructed object before the exception occurrence point in the same scope will also be destructed (isn't it even more amazing !). Therefore, it also shows that C ++ Exception Handling does not undermine the object-oriented features specified in the C ++ standard. When an object has a scope, it must be parsed, even if it does not have any exceptions, in short, no matter whether it is a normal execution process that causes the object to exit the scope, or if an exception occurs when other objects are running, it causes itself to exit the scope;

(3) When an exception occurs in the member function, objects in other scopes that are not affected will keep their original execution processes.

Summary of exceptions thrown by object member functions

Haha ^-^, there is only one sentence, that is, "C ++ Exception Handling will not destroy any object-oriented feature !", Therefore, the hero a Yu suggests that you do not need to remember the N conclusions summarized above. Remember this one!

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.