Exam4Training

What will happen if we use the operator “new” and the memory cannot be allocated?

Given:

#include <iostream>

#include <exception>

using namespace std;

int main () {

try

{

int * myarray= new int[1000];

}

catch (bad_alloc&)

{

cout << "Error allocating memory";

}

catch (exception& e)

{

cout << "Standard exception";

}

catch (…)

{

cout << "Unknown exception";

}

return 0;

}

What will happen if we use the operator “new” and the memory cannot be allocated?
A . It prints: Error allocating memory
B . It prints: Standard exception
C . It prints: Unknown exception
D . Compilation error

Answer: A

Exit mobile version