3 ms·
Once upon a time I hated exceptions, but today I like them more and more for each day. The hate was something I inherited from the early languages I worked with
by fetbaffe 9y ago
Once upon a time I hated exceptions, but today I like them more and more for each day. The hate was something I inherited from the early languages I worked with where exceptions where uncommon and was considered de facto wrong, it was the C approach of error handling.
What I learned working on a medium-sized web project is that you have too few exception types. Yes, create more unique exceptions for each situation and avoid reusing them across the project.
Too general exceptions make your code base suck hard. Complexity of exceptions with small benefit because you can't discriminate between them. ObjectNotFoundException is pretty useless but AccountNotFoundException is much better. Don't be afraid of types, use it to your advantage.
Optimize the happy path and fail fast on the sad path.
Note, have not tried this approach on a C++ project yet, maybe it will not work as well there.
- AstralStorm 9y agoWorks just as well until one of the types leaks accidentally by confusion. This is why you should have your exception types derive from common ones so that they can be reasonably caught by general handlers.