异常处理

来源:互联网 发布:web数据挖掘 pdf下载 编辑:程序博客网 时间:2024/04/29 12:55

异常处理


简历稳定的系统

为了防止资源泄露, 用以下两种方式来防止 “不成熟的” 的资源分配
1.  在构造函数中捕获异常,用于释放资源。
2. 在对象的构造函数中分配资源, 并且在析构函数中释放资源


以下代码是不成熟的分配方式,  极易造成问题:
#include "stdafx.h"
#include <iostream>
#include <cstddef>

using namespace std;

class Cat
{
public:
Cat()
{
cout<<"Cat()"<<endl;
}

~Cat()
{
cout<<"~Cat()"<<endl;
}

void g() {}
};

class Dog
{
public:
// 重载new 运算符
void* operator new[] (size_t)
{
cout<<"Allocating a Dog"<<endl;
throw 47;
}

// 重载delete运算符
void operator delete[] (void* p)
{
cout<<"Deallocating a Dog"<<endl;
::operator delete[] (p);
}
};

class UseResources
{
Cat*  cats;
Dog* dog;

public:
UseResources(int count = 1)
{
cout<<"UseResources()"<<endl;
cats = new Cat[count];
dog  = new Dog;
}

~UseResources()
{
cout<<"~UseResource()"<<endl;
delete [] cats;
delete Dog;
}
};


int _tmain(int argc, _TCHAR* argv[])
{
try
{
UseResources ur(3);
}
catch(int)
{
cout<<"inside handler"<<endl;
}
catch(...)
{
cout<<"inside catch(....)"<<endl;
}
system("pause");
return 0;
}




程序的输出结果是:

UseResources()
Cat()
Cat()
Cat()
allocating a Dog
inside handler

程序执行到UserResources构造函数中, Cat的构造函数成功的完成数组对象中的三个对象,然而在  Dog::operator new()
函数中抛出一个异常。   程序执行异常处理器之时突然中止,UseResources的析构函数没有被调用。这是正确的,
但是 UseResources 的构造函数没有完成, 这说明在堆上的Cat对象是不会被销毁的。


为了防止资源泄露, 用以下两种方式来防止 “不成熟的” 的资源分配
1.  在构造函数中捕获异常,用于释放资源。
2. 在对象的构造函数中分配资源, 并且在析构函数中释放资源
#include "stdafx.h"
#include <iostream>
#include <cstddef>


// 在构造中分配资源
template<class T, int sz = 1>
class PWrap
{
      T* ptr ;

public:
      class RangeError {};
      PWrap()
     {
           // 在构造中分配资源
           ptr = new T[ sz];
           cout<<"PWrap constructor" <<endl;
     }
     ~ PWrap()
     {
           // 在析构中释放资源
           delete [] ptr ;
           cout<<"PWrap destructor" <<endl;
     }

      T& operator [](int i) throw (RangeError)
     {
           // 返回下标的引用值
           if(i >= 0 && i < sz)
               return ptr [i];

           throw RangeError ();
     }
};

class Cat
{
public:
      Cat()
     {
           cout<<"Cat()" <<endl;
     }

     ~ Cat()
     {
           cout<<"~Cat()" <<endl;
     }

      void g () {}
};

class Dog
{
public:
      void* operator new[] ( size_t)
     {
           cout<<"Allocating a Dog" <<endl;
           throw 47;
     }

      void operator delete[] ( void* p )
     {
           cout<<"Deallocating a Dog" <<endl;
          :: operator delete [] (p);
     }
};

class UseResources
{
      PWrap<Cat , 3> cats;
      PWrap<Dog >    dog;

public:
      UseResources()
     {
           cout<<"UseResources()" <<endl;
     }

     ~ UseResources()
     {
           cout<<"~UseResource()" <<endl;
     }

      void f ()
     {
           cats[1].g ();
     }
};


int _tmain (int argc, _TCHAR * argv[])
{
      try
     {
           UseResources ur ;
     }
      catch(int )
     {
           cout<<"inside handler" <<endl;
     }
      catch(...)
     {
           cout<<"inside catch(....)" <<endl;
     }
     
      system("pause" );
      return 0;
}
  



0 0
原创粉丝点击