In my recent work, I encountered this problem twice. One was a monthly communication with the Manager. When the Manager asked me this question, I had to handle it; another time at the "Code View" Study Group meeting, my colleagues asked this question. So simply sort it out so that you can be clear.
The first is the destructor.
I. destructor
Refer to clause 08 in Objective C ++: do not let exceptions escape the destructor.
Summary:
1 .! Although C ++ does not prohibit Destructor from throwing exceptions, this may lead to premature termination of the program or ambiguous behavior.
2. If an operation may throw an exception, the class should provide a common function (rather than a destructor) to execute this operation. The purpose is to give the customer a chance to handle errors.
3. if exceptions in the Destructor are not indispensable, try catch to swallow the exceptions. However, this method is not good. We recommend that you report errors earlier.
Ii. Constructor
Summary:
1. But the memory resources applied for by the object itself will be released by the system (the internal member variables applied for the resource will be called by the system in reverse order ).
2. Because the Destructor cannot be called.
3. the constructor can throw an exception, but you must ensure that the system resources are released before the constructor throws an exception to prevent memory leakage. (How to guarantee ??? Use auto_ptr ???)
Summary:
1. Do not throw an exception in the constructor as much as possible. If necessary, avoid Memory leakage!
2. Do not throw an exception in the Destructor!
Reference:
1. Objective C ++ clause 08: do not let exceptions escape the destructor.
2. Exceptions thrown by the C ++ Constructor
Http://blog.csdn.net/deyili/article/details/6332760
3. Will Memory leakage occur when a C ++ constructor throws an exception? :
Http://blog.csdn.net/sxf_824/article/details/4926687
4. Can an exception be thrown in the constructor? What about destructor?
Http://blog.csdn.net/panlong1987/article/details/1835454
In the future, we should stick to such a small summary. If you encounter problems that you do not understand every day, we should make it clear in time, so we cannot make it a good night! Come on, good night