- Does using operator new in c# might fail (if it requires a large memory for example)? -Solved-
- And how to discover it? -Solved-
- What other failures new operator might throw?
Thanks
Thanks
Does using operator new in c# might fail (if it requires a large memory)?
Yes. (The resource you are likely to run out of is address space, not memory per se.)
And how to discover it?
I don't understand the question.
If new
fails it will throw OutOfMemoryException. Additionally the constructor itself may throw any exception depending on the implementation.
From the MSDN documentation for OutOfMemoryException:
The following Microsoft intermediate (MSIL) instructions throw OutOfMemoryException :
box
newarr
newobj
A new
operator that invokes a constructor can throw any exception that you can imagine. Fro example, if inside the constructor it tries to allocate something big and fails, then that exception might be caught and re-raised as something more exotic.
Of course, at the point when you start seeing out-of-memory you should probably consider the process terminally ill, and put it out of its misery ASAP.
Constructors can also, despite all the rumours to the contrary, return null
even for classes - but that is an extreme edge-case, bordering on the pathological.