resource acquisition failure handling [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this q开发者_如何学PythonuestionAfter years of programming, I havent had a situation where reasonable malloc or new would fail (maybe because my mallocs are trully reasonable), though I always check for it.
In my case, apps should gracefully (i hope) close with an appropriate log entry. What would you do in this case? Its interesting to hear your approach - do you wait for resources or close the shop?
I usually have my program shut-down as gracefully as it can, with simple logging of the error message. In C++ I do this by having a catch
for std::bad_alloc
in main()
. By the time the catch
executes, destructors called by stack unwinding should have freed some memory, so the logging itself is less likely to fail. I avoid memory allocation (for example by using char *
strings rather than std::string
strings) in that logging code, to further reduce the chance of the logging failing.
There's pretty much nothing you can do if dynamic allocation fails- there are pretty much no operations written to handle that situation. If it fails, then just let the app crash.
精彩评论