Throw an exception while construct an object

来源:互联网 发布:数控编程用什么软件好 编辑:程序博客网 时间:2024/04/30 02:35

while reading the book < thinking in c++ 2>, I got the codes below:

#include <stdlib.h>
#include <iostream>

#pragma warning(disable : 4297)

using namespace std;

class buffer
{
public:
 explicit buffer(size_t) throw();
 ~buffer() throw();
private:
 char *const p;
};

buffer::buffer(size_t const count)
try
: p(new char[count])
{
 cout << "constructor goes well......" << endl;
}
catch (...)
{
 throw "hello";
 abort();
}

buffer::~buffer()
{
 delete[] p;
 cout << "destructor also goes well......" << endl;
}

static void do_something_with(buffer &) throw()
{
 cout << "something is done !" << endl;
}

int main()
{
 buffer b(100);
 do_something_with(b);
 return 0;
}

原创粉丝点击