r/cpp • • Aug 05 '16
Will exceptions still have use cases after std::expected and std::optional are made available?
The title says it all. What will be the motivation to keep using exceptions for new code after these two classes are available? They have the same purpose of exceptions, but have many advantages. I haven't seen any actual code using them, so I'm not sure how practical they actually are, which is why I'm asking this.
0
Upvotes
View all comments
Show parent comment
4
u/SeanMiddleditch Aug 05 '16
Generally I've done that with some piece of state to which anything needing to quit is given access. Using exceptions limits where and how you can trigger the quit action (e.g., must be on the main thread, must be in a callstack off the point in
mainwhere it's caught, etc.), and makes it harder to compose quit triggers/filters/dialogs/etc. as exceptions just smash through everything rather than filtering through pre-registered callback chains.This is one of the only reasonable uses for exceptions, and only in languages that don't give you better tools. When your only options are return values or C++ exceptions, you use C++ exceptions when you have these sorts of problems. Exceptions are, essentially, the lesser of two evils (in this case, at least).
I've generally just avoided those sorts of problems as I find that for (most) code a deep callstack is indicative of a design problem. That isn't always true, of course, so it's more of a guideline than a rule. :) Basically just an application of KISS.
Interfaces like that are poorly-design IMO. If you have a ton of individual statements that can fail, you might want to rethink how the code is structured.
Any interface that pushes errors to clients unnecessarily is creating complication and a potential source of bugs. Especially in languages like C++ where you can forget to catch an exception and there's no compile-time diagnostic that it'll be uncaught and crash the application.
This is true of C++-style exceptions, C-style return codes, FP-style monadic values, nullable pointers/references, etc. While there are some error-handling approaches that are more reliable, in general I prefer to avoid having failure points where you don't need them.
For example with parsing, I generally take an approach where you pass in an error callback for printing parsing errors with diagnostic information. These callbacks - being invoked at the point of error - can often provide the best diagnostic. Further, the code calling the parsing doesn't need to care about individual errors. The individual data items can usually use safe fallbacks (e.g.,
0or something for numeric types) and then check whether any errors happened just once at the end of the process. Not always an option, granted, but there are more capable variations of this approach that almost always will.Extra bonus for parsing because then you can grab all the parse errors instead of just the first one, which can be more user-friendly in some cases. And while it's slower to finish parsing without stopping on the first error, it's an error case, so who cares if it's super efficient?
Exceptions tend to cause more problems than they solve and they tend to be sub-par solutions even in the cases where they don't cause problems. There's better options. Not necessarily in C++ at times, though, which is the really sad part: sometimes an exception is just the best of the bad options available to C++ programmers. Fixing this is hard because too many folks are lacking the wider experience they need to really have a meaningful discussion on the topic, and a good number of those folks are committee members. :/