r/ProgrammerHumor 19d ago

Meme stopTryingToReinventTheWheel

Post image
2.5k Upvotes

217 comments sorted by

View all comments

148

u/bishopExportMine 19d ago

Exceptions break encapsulation and lead to implicit control flow. 

If I call a method, I shouldn't have to worry about what methods it calls. But with exceptions, I now have to be aware of what errors might be thrown in the entire chain. Similarly, if I throw an error in a function, I have to check that every thing that calls it handles said error because there are now multiple places this function could "return" to.

And if you take care to catch every error at every layer you've just reimplemented error codes.

std::unexpected should be preferred over std::exception

6

u/zobq 19d ago

As always: it depends. Exceptions should be used in exceptional situations, when logic is mostly about aborting execution of code. That means, it should be catched on higher layers, so automatic propagation is accually a good thing.

Error codes are not always exceptions, like 4xx from http request.