You can use new without exceptions, but you can't detect OOM then, if I'm not mistaken.
> I think RAII is more about destructors (which must never throw exceptions). You can certainly write RAII style without throwing constructors. Generally, exceptions depend on RAII, but not the other way round.
The whole idea of RAiI is that you Acquire a Resource in the constructor (during Initialization), so that the destructor can guarantee it gets de-allocated.
If you construct your object and then acquire a resource in it later, from a function that can return an error without throwing, then you introduce complexity in the destructor and may risk errors more.
Yes, that's the meaning of the acronym means, but unfortunately it's really a misnomer.
When we say RAII style programming, we rather mean scoped destruction to avoid manual resource management. A classic example would be using std::lock_guard over a raw std::mutex. Some RAII objects don't throw on resource acquisition failure (std::fstream) or they expect that resource acquisition can fail (std::unique_lock with std::try_to_lock tag). Other RAII objects might not require any resource in the constructor at al, instead their only job is to release the resource that is given to them, like std::unique_ptr.
AFAIK `new (std::nothrow) T` returns nullptr if allocation fails, like std::malloc.
You can detect the same errors with and without exceptions - the nothrow variant returns nullptr on error. Due to overcommit, neither can be relied on to detect OOM in practice.
> If you construct your object and then acquire a resource in it later, from a function that can return an error without throwing, then you introduce complexity in the destructor and may risk errors more.
Not neccessrily, e.g. free and delete are both no-ops for nullptr.
> but then you can't use `new`
You can certainly use new without exceptions, see https://en.cppreference.com/w/cpp/memory/new/nothrow or https://stackoverflow.com/a/15292148
> and without constructors you can't use RAII, and so on).
I think RAII is more about destructors (which must never throw exceptions). You can certainly write RAII style without throwing constructors. Generally, exceptions depend on RAII, but not the other way round.