标准异常

#include <iostream>
#include <new>   // new头文件表示标准异常bad_alloc

using namespace std;

class Dog
{
public:
	Dog()
	{
		parr = new int[1000000];  // 4MB
	}
private:
	int *parr;
};

int main()
{
	Dog *pDog;
	try
	{
		for(int i = 0; i < 1000; i++)
		{
			pDog = new Dog();
			cout << i << ": new Dog 成功," << endl;
		}
	}
	catch(bad_alloc err)
	{
		cout << "new Dog 失败:" << err.what() << endl;
	}

	return 0;
}

你可能感兴趣的:(标准异常)