Idea: manage resources with local objects. When an exception occurs during a call, use the policy that must be called by the destructor of a local object to release resources in the destructor. A local object stores [resources that must be automatically returned] and returns resources using the object's destructor. Auto_ptr, scoped_ptr, auto_lock, FilePtr, and HandlePtr are all of these ideas. This is the sample code. [Cpp] # include # Include # Include Using namespace std;/* Author: lpstudy Date: 2013-3-16 content: more efficient C ++ exception chapter 9th, using partial object destructor to release resources, ensure that resources are safely released when exceptions occur. */# Define TRACE_FUCTION_AND_LINE (fmt ,...) printf ("[% 20 s: % 4d]" fmt "\ n" ,__ FUNCTION __, _ LINE __, ##__ VA_ARGS __) // Widget is just a simple test class and serves as an instantiation parameter of the auto_ptr template. Auto_ptr is used to manage the new Widget memory and delete class widgets {public: Widget (int nHight = 0): m_nHight (nHight) {TRACE_FUCTION_AND_LINE ();}~ Widget () {TRACE_FUCTION_AND_LINE ();} void Log () {TRACE_FUCTION_AND_LINE ("My Log ------ % d", m_nHight);} int m_nHight ;}; // FilePtr receives a FILE handle and closes the handle when the object is parsed. Class FilePtr {public: explicit FilePtr (FILE * f = NULL): m_hFile (f) {TRACE_FUCTION_AND_LINE ("m_hFile: % 08 p", m_hFile );}~ FilePtr () {if (m_hFile) {TRACE_FUCTION_AND_LINE ("close m_hFile: % 08 p", m_hFile); fclose (m_hFile) ;}} FILE * Get () {return m_hFile ;} private: FILE * m_hFile;}; int main () {TRACE_FUCTION_AND_LINE ("Trace auto_ptr ....... "); auto_ptr AutoWight (new Widget (100 )); AutoWight-> Log (); TRACE_FUCTION_AND_LINE ("Trace FilePtr ....... "); FilePtr filePtr (fopen (" data.txt "," a + "); char buf [100] =" Data Test... \ n "; fwrite (buf, 1, strlen (buf), filePtr. get (); return 0;} I hope I have not obtained this idea, or I have not fully understood this idea. Let's take a closer look at this simple example. If you want to learn more about auto_ptr and lock, we recommend that you read scoped_ptr and autolock.