6 ms·
He makes the point that exceptions should be truly exceptional conditions for your program that would prevent it from running, generally due to system malfuncti
by edoo 7y ago
He makes the point that exceptions should be truly exceptional conditions for your program that would prevent it from running, generally due to system malfunction. Some people use it for basic business logic and it was never meant for that. It is meant to help you gracefully abort things when everything is about to go to hell.
- PaulDavisThe1st 7y agoThis is not an accurate summary of what C++ exceptions where "meant for". Here's Bjarne in 1989: "The author of a library can detect errors, but does not in general have any idea what to do about them. The user of a library may know how to cope with such errors, but cannot detect them – or else they would have been handled in the user’s code and not left for the library to find. The primary purpose of the exception handling mechanism described here is to cope with this problem for C++ programs; other uses of what has been called exception handling in the literature are considered secondary. See the references for discussions of exception handling techniques and mechanisms."
- edoo 7y agoI think it depends what you consider an error. For example let's say I have a library that opens sockets to hosts. If I can't reach the host that isn't an error and I report back a failure of that function to succeed. If I can't allocate the memory for a socket then instead of reporting a failure to connect I throw an exception for the true error.
- quietbritishjim 7y ago> He makes the point that exceptions should be truly exceptional conditions The comment did say that, but the only explanation of why exceptions should only be used that way seemed to be performance based. If that is not the issue, then what is? Saying "that's what they're for" without any other detail is a circular argument.
- gpderetta 7y agoIt is a circular argument. I for one I would use exceptions significantly more often if throwing and catching them were to be cheaper (also syntactically). As it is you often end up with both a throwing and optional-returning non-throwing variants of many functions.
- fefe23 7y agoI feel slightly misrepresented. I explained how exceptions are implemented, and the actionable intelligence you can derive from that understanding (if perf matters to your code and/or scenario, then use exceptions only for hard errors, not for generic signaling). If perf does not matter in your scenario, than obviously the guidance does not apply to you. To me that does not mean it's a waste of time to explain how exceptions work and what that means to performance critical code.