how to catch out of memory exception in c++?

c++, exception, try-catch

Solution

Catch `std::bad_alloc`.

You will also need a strategy for handling the errors, since many of the things you'd like to do will require memory (even if it's only to display an error to the user before shutting down). One strategy is to allocate a block of memory at startup, and `delete` it in the exception handler before attempting to use more memory, so that there is some available to use.

Problem

can anyone please tell me how to catch out of memory exception? for ex. ``` try { while(true) { int i = new int; } } catch( ? <--- what should be put here?) { //exception handling } ``` and also this, ``` queue<int> q; try { while(true) { q.push(10); } } catch( ? <---- what should be put here?) { //error handling } ```

Original source

Related problems