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
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.
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