//验证动态分配内存失败,引发的异常
#include<iostream>
#include<new>
using std::cout;
using std::endl;
using std::bad_alloc;
int main()
{
char* pdata=0;
size_t count=~static_cast<size_t>(0)/2;
try
{
pdata=new char[count];//如果内存申请失败,则引发下面的异常
cout<<"Memory allocated. "<<endl;
}
catch(bad_alloc &ex)
{
cout<<"Memory allocation failed. "<<endl
<<"The information from the exception object is: "
<<ex.what()<<endl;//ex.what() 输出引常的原因。
}
delete[] pdata;
return 0;
}
//验证动态分配内存失败,引发的异常
最新推荐文章于 2021-06-28 16:50:14 发布