3 ms·
You can't really tell if the stack overflowed in a constructor though, plus that will essentially /never/ happen, so I think it's okay to exclude stack overflow
by PeCaN 10y ago
You can't really tell if the stack overflowed in a constructor though, plus that will essentially /never/ happen, so I think it's okay to exclude stack overflows when creating stack allocated objects as failure conditions. In that case no, a lot of constructors won't be able to fail.
- comex 10y agoAny function can overflow the stack. If the object is constructed on the heap, 'new' can fail but will always throw an exception; if you aren't using exceptions it will just crash the process.
- PeCaN 10y agoYeah. That's why I don't say it's a failure condition for a stack-allocated object to overflow the stack. Notice my parent comment said: > Constructors are resource allocation, so they can always fail. Which is false.
- robotresearcher 10y agoCan you explain why this is false? If I run out of stack or heap by exceeding the process limit, the constructor must fail. Is this incorrect? I see your comment above that stack overflows essentially never happen, but having watched it happen, I disagree.
- ori_b 10y agoThis is not an error that you can reasonably recover from. If you are out of stack, even constructing an exception to throw is likely to fail. Because the only correct behavior here is to terminate the program immediately, it doesn't matter in the discussion of whether to use exceptions or not.
- lmm 10y agonew can and does fail when you're out of memory, and this is a condition that many programs do need to handle (for some programs crashing is appropriate and in that case sure, you can live without exceptions). In a pedantic technical sense that's not the constructor failing, but your options for handling it are much the same.