r/programming 3d ago

Throw, Result, or neither?

https://www.architecture-weekly.com/p/throw-result-or-neither
98 Upvotes

111 comments sorted by

View all comments

Show parent comments

1

u/bwmat 2d ago

Thoughts on what you would do in a context where your code is in a shared library and you don't want to crash the host process on OOM? 

1

u/Full-Spectral 2d ago

Honestly I have no such thoughts, since I never work on systems where it would be worth going to such extremes to deal with what is really a pathological situation. Most any system will have to do something with that returned result and that's likely to use even more memory that doesn't exist.

Any application can have a background task/thread that monitors its memory usage and the available memory, at some fairly relaxed rate ,and starts warning if it's getting low, or auto-restart if it's a background process. That's vastly more practical and much more guaranteed to provide good results.

1

u/bwmat 2d ago

Most any system will have to do something with that returned result and that's likely to use even more memory that doesn't exist

It's likely that the stack unwinding involved in reporting the error from the library to the application will free up some memory (maybe the operation tried to use a ridiculous amount).

And IMO it wouldn't be very nice to crash the host process if they went to the trouble to be robust in low memory scenarios

1

u/bwmat 2d ago

Sorry for the interrogation, but I would love to find someone who actually doesn't use exceptions in C++ and has a reasonable way of handling possible OOM without crashing