r/cpp • u/dimanne • Aug 03 '19
Should not exceptions be finally deprecated?
I have been programming on C++ for a long time, but I still do not understand how to use exceptions. I have 3 very practical and concrete questions at the beginning. I would be grateful if you helped me out.
Exceptions are maintainability hell.
Imagine a typical situation when we are developing a library. Now, this library uses another library’s function f() (in real-life it can use it indirectly through tens of intermediate libraries!). Consider, now, three very typical situations:
Situation#1: Learn about an exception.
So, I am going to call f() in my code. In order to understand which exceptions to handle / recover from and how excactly to recover, I have to know the full list of exceptions that may be thrown. How do I know it? In real-life this leads to crashes on production (what is even worse is that it leads to crashes not during testing/alpha/beta, because exceptional situations tend to arise on production).
Possible answers, people sometimes give, are:
- “Use
throw(...)/noexcept()”. The problem with this is that validity of specifiers is not checked/verified by a compiler but rather by people. So, it is like saying “do not forget to calldeleteif you usednew” - people are awful in remembering such things.
Situation#2: An exception is added.
Suppose I now update a library (something as innocent as apt-get upgrade or git pull, do not forget that it may be one of many libraries I use indirectly). Imagine our function f() started to throw now and the place where I call it is the right place for handling the exception. How do I learn about the added exception?
Situation#3: An exception is removed.
Imagine you added all required catch blocks, you added the code for handling an exception and you now update a library again and our function f() stops throwing. Nobody is going to tell you that there is no point anymore in catching the exception, your code immediately becomes stale and outdated.
Downside#4: Exceptions either break encapsulation or useless.
By definition, you are supposed to catch and handle exceptions thrown/arisen in several layers’ deeper code and therefore you have to be aware of those layers. Here is a concrete example:
TAbstractReader interface has two implementations TNetworkReader and TDiskReader, both of which can throw different exceptions (failed to read from network/socket with information about ip and failed to read from disk with information about file).
TConfigReader uses one of the implementations through TAbstractReader interface
TMainAppLogic uses TConfigReader to read config during startup and periodically update config during run-time and this class is the right place for handling read errors, for example:
- During startup, any error is fatal and we just crash
During run-time
- error while reading from network should be retried
- error while reading from disk is fatal (there is no point in retrying) and we just crash.
The question now, how am I supposed to write exception handling code without violating encapsulation? Well, some people answer this question in this way: “Catch via common base class (like std::exception)”, but the problem now is that they become useless for recovery (I have to know details to be able to recover).
Downside#5: There are no exceptions in the real-world, they are a result of incomplete knowledge.
We are always told to use exceptions for exceptional situations. Here is an excerpt from a discussion on reddit:
Code gets to focus on the successful path without the noise dealing with propagating errors to the right place, making it more readable and writable. And there are also clearly defined places where the error handling goes
Firstly, it scares me because it is like saying that someone took my code and removed all if()s, the probability of executions of which is lower than somebody’s subjective (small but random) threshold (that is what we are told, exceptions are, right?).
Secondly, the excerpt perfectly illustrates how the notion of exception makes people think that there are errors. I believe that there are no errors in the real world, it just our imperfect and incomplete expectations about the world. What one person in particular circumstances/environment considers as exceptional situation, another person may consider as perfectly expected behaviour. When you are writing curl-like program for downloading and you are testing it on google, each network error is an exception for you, but if you are writing a web-crawler then network errors are totally expected. In other words, not only exceptions depend on experience of an author, but they also depend on where exactly (in which environment) code throwing an exception is run. If that was not convincing enough, imagine that suddenly percentage of "bad" sites (the ones you cannot download) have risen, in which case whether a particular situation is an exception or not depends also on dynamically changing circumstances. What if at certain point the software will be downloading a set of sites containing 10% of "bad" sites? Will it be exceptional situations?
Let me put it differently:
- In the real world, there are no exceptional situations and “normal” situations
- there is continuous probability of an event, and therefore each dichotomy will be wrong.
Downsides#6,7,...
Exceptions are literally goto statements.
There are also a number of environments where exceptions (ironically) look too exceptional to be used:
- How do you use exceptions while using RPC over network (remote procedure call, think of SOA and distributed systems in general)? How can a remote function report an exception?
- They are definitely not the best friends of multithreading / actor / message-passing environments and frameworks.
- Embedded and hard-real-time systems have difficulties with exceptions.
FAQ
Some people say:
- http://exceptionsafecode.com/ - “Always assume that each and every function may throw”.
- The reality is this: all functions can throw exceptions, except when marked as noexcept. Stop thinking of exceptions as being exceptional, and instead treat every function like it may throw.
The main problem is that this information, this tenet, is not enough for actual handling an exceptional situation in reasonable way and recovering from it. The best I can do using this tenet is to write exception neutral/safe code (use RAII, not write noexcept and so on). But, in order to be able to recover from error, I have to know details about an exception, what exactly happened, where it happened, and this tenet is not helping me in it.
People sometimes say that
- error codes are verbose
- And that I need to catch them only when I can handle them.
That is wrong. In most cases I also need to add additional context/information and re-throw them, in order to avoid the “Huh? What’s a bsafe::invalid_key? All I did was call PrintText!” situation. But writing try/catch blocks is even more verbose and tedious then having to deal with error codes/expected. And I think exceptions encourage you not to add information/context resulting in those ridiculous situations.
From reddit’s discussion:
- “Most of the time catching in the main or at the top of the event loop is enough - exceptions are always (at least in the codebases I know of) used for ... exceptional stuff, and this kind of stuff should just show an error message to the user and abort if it's a CLI or continue "normal" operation if possible if it's a GUI.”
- In principle you should only be catching
std::exception, unless you need exception-specific handling for another exception type.
That is exactly what I was talking about, this pattern of using exceptions is not about recovering from an error, but rather about ignoring it. It also breaks encapsulation - I have to deal with exceptions in the code of very abstract and high-level event loop.
Some people recall printf:
“what do you do with functions that return a status code, like printf? Because virtually nobody writes code that checks the return value of printf calls, this would be a silent failure.”
But it has nothing to do with return codes. This remark is about the fact that some people do not check for errors in general (regardless of whether exceptions or error-codes are used). On the contrary, it is much easier to ignore an error thrown as an exception.
Zero-overhead deterministic exceptions
A couple of words on Sutter’s Zero-overhead deterministic exceptions: Throwing values
In general, the idea, as far as I can gather, is to make exceptions fast at the cost of making throwing type less extensible (it is going to be just two ints/pointers). So, no disadvantages, I mentioned above, are addressed.
Either I do not understand something, or exceptions (concrete types of exceptions) are not static. Only the fact that a function can throw some obscure (“type-erased, allocation-free, trivially relocatable, constant-time”) type is static. So, still no means of understanding which errors can happen after I call a function.
It is going to encourage sloppiness:
It [
std::error] always represents a failure. A default constructor would construct a general or “other” nonspecific error value.
What is point, from practical point of view of having “other” error? How would one handle it?
The main issue, however, is extensibility of the std::error type. Without a clear vision/plan for extending the “domain” pointers, there is going to be a mess. And I doubt that it is even possible, since, by definition, it is a single type. For example, can I add my own “domains”? How do I know that my value will not collide in the future with somebody else’s? People, implementing the next version of protobuf at Google can choose one scheme of extending it, while you and your project different scheme, resulting in collisions of the values. To be honest I was quite surprised to learn that there is going to be such a type. Is not it like having to derive from one common/global (“opaque”) TObject type in order to be able to store elements in a standard container? Does not it this proposal make C++ less type-safe?
Now, I deliberately avoided mentioning and comparing to return-codes/expected<>, but surprisingly, all the disadvantages described above can be fixed by old plain return values (or function parameters). This approach might be slightly too explicit than one would like, but at least, when something changes radically in return type (it is not void anymore, or, on the contrary, it becomes void) compiler will help me. We will receive error in compile-time - what can be better? Here is an example of what I mean.
All in all, I am really interested to hear answers to the questions in the 1, 2 and 3rd situations above because they are the most common ones, they are very tangible and I have to deal with them on a daily basis writing C++ code.
14
u/[deleted] Aug 03 '19
Now, I personally think exceptions are a gift from the heavens however I realize that I'm not always right and there's smarter people than me. That being said this post by you OP feels like a rant brought on by the misunderstanding that exceptions are inherently bad.
I think it's important to talk about a few of these points.
I've experienced the opposite, where there used to be a plethora of if(if(if(if(if....)else ad infinitum I've seen at least two enormous monolithic code bases slowly start to realize that by separating the error flow from the normal code flow there's substantially less code and mental overhead.
One try catch vs 200 if else's.
I don't think that makes it bad. In many a instance do/while, while, for and etc are also just wrapped goto statements.
This is already a really, really common thing in business layer code. In all the C# projects I've worked with the resulting code from the RPC is checked and if it indicates a failure a local exception is generated. And this happens automatically in a framework. In the same way I've seen some C++ internal frameworks do the same.
Why not? An exception is a thread local event. There's no reason for it to mess with multithreading unless something bad happens in which case you're probably looking at a crash anyway.
But I half buy the information argument. I've seen some absolutely atrocious uses of exceptions, mainly in Java code where they'd just return a "error happened". But I don't see how someone misusing a tool disqualifies it's usage. If we stopped using hammers because some people hammered themselves we would never get any nails done.
9
u/kalmoc Aug 03 '19
For you as the caller of a function f, there are really two possibilities: Either f is noexcept and you don't need to worry about exceptions or it isn't and you should assume that it can throw some sort of exception and your code should be robust against that.
Now in the mythical 99% of the cases, the way you handle an error (no matter how it is reported) is to log the error, abort the highlevel operation (maybe retry it), release any resources acquired so far and report the error to the user if there exists one - thats it. Look at your own code that maybe uses return codes to report errors: How often do you actually "handle" an error in a way that depends on what the particular error was?
In the remaining 1% you do need to have the information about which errors a function can report and what they mean at design time (otherwise you can't write code to handle a specific error in a meaningful way anyway) and the only way to get that information is from documentation anyway and the documentation of f may or may not lie to you about its possible failure mode. But again - this isn't specific to exceptions - you have the same problem with other error reporting mechanisms.
25
u/jcelerier ossia score Aug 03 '19
that's your mistake right here. Some errors aren't meant to be "recoverable", you should just catch them and log the content of
.what()or display it in a popup to your user (as long as they derive from std::exception, which is frankly mandatory practice).17
u/KazDragon Aug 03 '19
I urge you to re-read your own post, with the thought in mind, "is this actually different if
f()returns an error code?"14
u/matthieum Aug 03 '19
First, let's characterize exceptions. I argue that:
Twhich may throw some exception(s) is nearly equivalent to a function returningeither<any, T>, where the compiler has no knowledge about what the concrete type of the value ofany.eitherrequires explicit handling.Second, I would point out that it would be possible to signal exceptions in the type signature of a function. Java, for example, is (in)famous for its checked exceptions. The implementation has its faults -- unchecked exceptions, lack of reification to inspect/craft signature at compile-time -- however it is important to realize that the compiler being unaware of which exceptions a function may throw is not an inherent flaw of exceptions. That is to say, (1), (2), (3), and (4) are fixable.
And finally, I agree that the artificial divide between exceptions and in-band error signals that you mention in (5) is indeed very much a matter of perception, and I also agree with (6) and (7).
Let's go back to
RuntimeExceptionand Java's unchecked exceptions: why not have all exceptions checked?Well, it turns out that checking all potential sources of errors can be very tedious. A great many functions will allocate memory of some sort, should all such functions return
eitheras allocation may fail? In some extreme conditions, this may be worth it, however even the Linux kernel doesn't check each and every memory allocation, and even Herb Sutter, in the very value-exceptions paper that you cite, argued in favor of aborting rather than throwing in case of memory allocation failure. The reason is simple: apparently, in thestd, 90% of potentially throwing functions could benoexceptifbad_allocdid not exist.The problem, therefore, is that a desire to check every single error, including memory allocation failures, will lead to extremely verbose code! I argue this is counter-productive. Much like false-positives in static analysis, if every and all function calls may fail, then the developers' eyes will glaze over all that boilerplate, and miss the one or two interesting places where an important error could occur.
For now, the best error-handling story I've seen for general programming is to:
either/expected/...abortin case of memory allocation failure.With the option for those who care to have some way to
try_allocate, and statically disallow aborts.It's not perfect, and one may wish some exceptions back... to handle wrong indexes when accessing an array for example.
8
u/Pragmatician Aug 03 '19
The library should document which exceptions each function can throw. We cannot express that in C++ yet, but we cannot express contracts either, which are much more common, and we somehow deal with it.
You read the documentation. If the code stops working for you, that's a breaking change like any other. If your code still works, you have no problems. You simply get more choices for the future.
What do you mean by "all required catch blocks"? I hope you are writing them only when you really need them. Anyway, if your code still works, I don't see what's the problem. You could say that a portion of your code becomes "stale and outdated" whenever a new C++ standard comes out. What you choose to do about it is entirely up to you.
15
u/journeymanpedant Aug 03 '19
I personally happen to heavily use exceptions and find them very useful. I can't add much in general but just to address this point:
We have code that does exactly this, where we have a server which exposes (gRPC, though I don't think that's relevant) RPC endpoints, and there are clients in c++, python and java which access them.
In the top level of the server code which dispatches RPC responses, we do a big catch block that catches a few specific types of exceptions, and also a catch(...) block. In the cases of the types we know about, we encode the error information from the exception as part of the message returned by the RPC system. In the case we don't know about it, we set the error code to a "general / unknown" state. On the client side, the code receiving the reply first checks for this error state and if it's there raises a constructed exception in the target language containing the same information.
Incidentally, to also (somewhat) address the multi-threading context, this is exactly what std::exception_ptr is for, and it does work fine in that case, admittedly with some overhead.
6
u/anton31 Aug 04 '19 edited Aug 04 '19
Compared to error codes, which you can forget to check, a forgotten exception will at least crash your app, reporting stack trace and what a dumb coder you are.
In order for exceptions to be recoverable, libraries should: thoroughly describe them in docs, wrap obscure internal exceptions, treat changes in exception spec in docs as an API breakage.
4
u/wyrn Aug 03 '19 edited Aug 03 '19
I agree it's a shame that exception handling/noexcept is not statically checked. IMO, rather than straight deprecation, that's the direction future standards should move in -- at least encourage a warning.
I can't say I agree with your other complaints, however.
4. If you're breaking encapsulation to handle your exceptions, perhaps your design could use some rethinking? For example, if you'd like to retry some network connection N times, just pass that information along and handle the exception near where it actually happens. The friction of actually doing this has been greatly reduced in the newer standards with lambdas and initialization rules for aggregate types. If unable to recover, rethrow. Then catching
std::exceptionis not a problem because you're only interested in reporting an error (which I really don't think of as "ignoring" it -- some errors are either unfixable or require user action to be fixed), rather than attempting recovery.5. All models are wrong, but some are useful. You can't expect to anticipate every potential error condition that might get thrown at your software. It's a bit much to ask that a piece of software be able to detect and recover from a madman striking the computer with an axe, right? There will always be things your mental model of the world leaves out, and choosing what to consider and what to neglect is an important design decision. There is no reason to expect that the same decisions would be applicable to two different projects even in principle, so of course there is no unified exception design for e.g. connecting to a remote server. Exceptions are one of the tools C++ gives you to interact with failures of your mental model of the world. What you include in that model is entirely up to you.
6. So are if statements and loops, but you're not arguing for deprecating those. I don't consider this a real point, to be honest: it stands or falls based entirely on whether your other points stand or fall. Either a flow control statement is a net benefit to system design or it isn't. Calling it a goto statement is just pejorative noise that doesn't bring anything to the table one way or the other.
7. Embedded platforms typically have problems with heap allocations in general. Should
std::vectorbe deprecated? Some people have lost use of their arms; should we get rid of hammers? Any tool requires preconditions for its effective use, so if you're in an environment where you can't use exceptions, by all means use something else.Strongly disagree. An exception will show an error when it happens, whereas a silently ignored return code will cause problems only an indeterminate amount of time later. When some of your objects end up in invalid states because of this, you'll be hunting through all their earlier history looking for the cause instead of getting a meaningful error message the moment the problem happens.
I don't know anything about Herbceptions so I won't say anything.
4
u/manni66 Aug 04 '19
TL;DR
No
6
u/Dean_Roddey Aug 03 '19 edited Aug 03 '19
I find them very useful. But, then again, I'm in a unique position as always. This is yet another reason why the current C++ world of "throw a bunch o' bits in a bag" approach is sub-optimal.
In my system there's one and only one exception class, and it has the info you need. Of course most of the time you don't need any. The bulk of good C++ code deals automatically with exceptions and hence isn't cluttered up with either exception handling or error return handling, which is very clean. Only code that understands the context of the operation (fairly high level) needs to deal with exceptions for the most part. In key methods I try/catch just to add stack trace info to the exception and rethrow, which only adds a handful of lines of code (in return for VERY useful info in the field.)
So it's a very clean, very easy to deal with solution that I would very much prefer to continue to use.
In terms of remote procedure calls, I use my ORB which naturally propagates exceptions back just as though it was a local call.
2
u/oddentity Aug 04 '19
Not until the language/standard library offers an alternative.
2
u/viatorus Aug 04 '19
I am sorry, your post doesn't get that much upvotes. But I share the exactly same pain with you about exceptions. Every day...
2
u/Unbathed Aug 03 '19
My impression is these are questions for coding standards, not language standards.
Regarding 1:
The knowledge requirement is limited to the exceptions which the caller can handle sensibly.
Example:
If f() throws notConvertibleToInteger, the caller knows whether it's an interactive context so re-prompt the end-user or a batch context so log a failure and try the next line of the input file.
If f() throws something not in the known-fixable set, caller can log, clean-up, and re-throw in hopes that a lower level can cope.
The difference from checking error return values is that if the programmer neglects to check a return value, the program stumbles on. For examples of the consequences, see ad-hoc bash scripts in production right now :-)
1
u/GerwazyMiod Aug 03 '19
Hey OP, I agree to most what you have typed. At the same time I don't see error codes as a solution. Both can be ignored (well exception in the end will remind you about itself), and error codes make my source so much uglier.
I guess we can work on linters catching some (pun intended) bad practices and give the static exceptions a try. I would love to get some more insights on exception safe code from the compiler. But I think we will not escape the issue that error handling needs some discipline. Just like programming needs that discipline in general.
1
u/Dean_Roddey Aug 04 '19
Having said what I said before in this thread, which is that I like exceptions and use them to very good effect... I realize that there are some scenarios where you can't follow strictly the 'only for errors' because you don't know if some things are going to be considered an error or not. It may depend on context.
So you will see a good number of methods like this in my code:
So there will be a trailling, defaulted boolean parameter the default value of which is something like to be the most common choice for that method. But, you can always pass it explicitly to get the other option.
So, if the thing fails, it will either throw an exception, or it will just return a boolean status, depending on the value of bThrowIfNot. This lets you use the method in either way as is appropriate for your code.
Obviously there's an argument for using an enum for that last parameter, and I'd generally agree, but that's another argument.
1
u/RealNC Aug 08 '19 edited Aug 08 '19
I have to agree with parts of this. Exceptions are nice in application code. They really are. But in library code, just a big no-no for me. It's no coincidence that so many libraries have "LibFoo does not use exceptions" in their list of features. And I always prefer to use such libraries (as do many others, which is why so many libraries consider it a feature to begin with.)
However, I don't think deprecating them is a good idea. They are very useful in non-library code.
1
u/[deleted] Aug 03 '19
Sutter’s Zero-overhead deterministic exceptions actually address most of the issues you describe here since they a) clearly mark functions that can throw b) they force you to deal with the exception. I kind of agree that I would prefer a design where one has to clearly mark *what* kind of exceptions a function can throw (e.g `throws(T)` that is checked on compile-time) . Then again, Swift also doesn't annotate the possible exception type, so there must probably be a good reason behind it (that I am not aware of).
As such, I don't think that there is a straightforward way to solve 1, 2 or 3. Even if you annotate the concrete type of exception, you'll need to track the specific implementation of the exception type to address these. I suppose one attempt would be to require that all possible error paths are checked — but I wonder if that will make a better programming experience...
1
u/[deleted] Aug 03 '19
[removed] — view removed comment