Let's say I have a class that looks like this:
Class A{
private:
int* A1;
int* A2;
public:
A(): A1(new int[1000]), A2(new int[1000]){}
~A(){delete[] A1; delete[] A2;}
}
I want to deal with a situation where the first allocation succeeds and the second fails, and std::bad_alloc
is thrown.
From what I gather, the destructor won't be called because the object was not created successfully.
This can be dealt with if I instead do:
A(): A1(new int[1000]), A2(new(nothrow) int[1000]){}
Then I will be able to deal with this case without exceptions.
However, from what I read nothrow
is very rarely used. I am not sure how exception can be used in this case.
Many thanks.