I'm reading The C++ Programming Language by Bjarne Stroustrup and I
was unclear as to the proper way to catch an exception thrown by the
new keyword. I was wondering if someobody here could let me know if I
have written the following code correctly.
<CODE_SNIP>
#include <iostream>
#include <new>
using std::cerr;
using std::bad_alloc;
int main()
{
char *p;
try
{
for(; ;)
p = new char[100000];
}
catch(bad_alloc)
{
cerr << "Error allocating memory...\n";
return 1;
}
delete p;
return 0;
}
</CODE_SNIP>
I thank you in advance for any pointers you can provide.
Sean