3 ms·
Bjarne's guidance has always been to use exceptions only in truly exceptional circumstances. A function that might occasionally fail on bad user input could ins
by ovao 9y ago
Bjarne's guidance has always been to use exceptions only in truly exceptional circumstances. A function that might occasionally fail on bad user input could instead return a std::optional and be marked noexcept (assuming it really doesn't throw). A function that fails to allocate memory, on the other hand, is truly exceptional, so throw std::bad_alloc.
The STL unfortunately doesn't always follow this approach, and tends to overthrow.
Signaling why something failed without exceptions is still unnecessarily tricky in C++. Not because it can't be done, but because there's no standard guidance on how to do it. There are 3,385 ways to do the job, so in many cases it's better simply not to.
- jgh 9y agosince optional is a thing now and the standards committee isn't afraid of verbosity maybe we can get std::value_or_error ;)
- throwaway84742 9y agoLike Google StatusOr<>?
- jgh 9y agoMaybe? I'll take a look at it!
- AstralStorm 9y agoLike std::future right? You can use that one today.
- blub 9y agoNope, a result<T,Err> like expected-lite on github.
- RotsiserMho 9y agoYou might be interested in Boost.Outcome (v2) or the proposed std::expected.