RAII vs. exceptions

后端 未结 7 1373
小鲜肉
小鲜肉 2021-01-29 21:54

The more we use RAII in C++, the more we find ourselves with destructors that do non-trivial deallocation. Now, deallocation (finalization, however you want to call it) can fail

7条回答
  •  遇见更好的自我
    2021-01-29 22:28

    One thing I would ask is, ignoring the question of termination and so on, what do you think an appropriate response is if your program can't close its DB connection, either due to normal destruction or exceptional destruction.

    You seem to rule out "merely logging" and are disinclined to terminate, so what do you think is the best thing to do?

    I think if we had an answer to that question then we would have a better idea of how to proceed.

    No strategy seems particularly obvious to me; apart from anything else, I don't really know what it means for closing a database connection to throw. What is the state of the connection if close() throws? Is it closed, still open, or indeterminate? And if it's indeterminate, is there any way for the program to revert to a known state?

    A destructor failing means that there was no way to undo the creation of an object; the only way to return the program to a known (safe) state is to tear down the entire process and start over.

提交回复
热议问题