can anyone please tell me how to catch out of memory exception?
for ex.
try { while(true) { int i = new int; } } catch( ? <---
catch (std::bad_alloc& ba){ cerr << "bad_alloc caught: " << ba.what() << endl; }
As a note you should read bdonlan's comment. The call to cerr may very well fail. Mark Ransom's suggestion in his answer is a good strategy to mitigate this issue.
cerr