5 ms·
IMO the reason not to use them isn't performance or size (although those were legitimate factors in the past AFAIK) -- rather that they make control flow very d
by arcticbull 10y ago
IMO the reason not to use them isn't performance or size (although those were legitimate factors in the past AFAIK) -- rather that they make control flow very difficult to reason about. They're worse than a go-to statement, they're a comes-from statement. They can pop out of anywhere and be handled anywhere up the stack. They always seem like a good idea at the time, and end up biting you in the butt.
Not to mention, that's old-school error handling, Result<T,E> types are what the kids seem into these days.
Curious how destructors will affect the numbers.
- saurik 10y agoThat result object is implemented in the same manner to an error code: you are burning cycles and registers on something that usually doesn't come up... all so you can clutter your code with what effectively amounts to a manual implementation of exception handling :/.
- arcticbull 10y agoThat's fair, though in my opinion it's more important to be clear and explicit in what's happening both for your own sake and for your future co-workers so they can understand what's happening and why than worrying about some minor (per the article) performance issues.
- nick_ 10y agoThat's what exceptions are for. They are a tool that _can_ be used to cut down on noise in code. Being clear and explicit about everything will leave your code looking like an implementation of the STL. The art of programming is partly about achieving a good signal-to-noise ratio.
- fmap 10y agoFrom a reasoning perspective throwing an exception is the same as returning from a function. If you want to reason about code which potentially throws an exception you have to keep track of a separate exception post condition. If you compile exception handling to separate return addresses this becomes rather obvious. In this case your control flow also stays reducible, which is commonly taken to mean "simple to reason about", and is very different from the kind of control flow that you can introduce using goto statements. I think the main problems with exception handling are cultural and psychological. Culturally we don't insist on proper documentation for exceptions. At best you'll see documentation for the functions throwing exceptions directly, but almost never for the functions using them a few levels up the stack... The situation is even worse with higher order functions for which people almost never document the exception behavior. Psychologically most people seem to ignore all but the most obvious exceptional cases. We really have to force ourselves to notice everything that could go wrong and to take a step back and rethink our designs if this question doesn't have a simple answer...
- mondoshawan 10y agoDocumenting and mentally reasoning about exceptions is only half the problem. The other half is scaling this discipline inside individual teams and scaling it out beyond as well. As a for instance, I can give instruction to my team to document exceptions when they are used, but during code review, I cannot guarantee that my engineers are being thorough enough to also document exceptions thrown deeper in the stack -- especially when dealing with a very large pre-existing codebase they didn't write and only have a passing familiarity with.
- baq 10y agodocumenting every possible exception becomes next to impossible if any function above in the stack decides to throw a new kind after a code change...
- marcosdumay 10y agoEven Java had to create some escape valve for its explicit exceptions, just because there was a huge set of them that could appear literally anywhere. But then, exchanging that for return values does not make your life easier in any way. It just means you will have to write redundant tests, and will have a flow that is exactly equivalent to the exceptions flow, after you simplify the redundancy down. You can have your compiler or IDE making the list of possible exceptions for you. But that list is always too big to reason about anyway, and for high order functions, it may be the entire set of exceptions available for your code and some more the it can't discover at compile time. Haskell has tried a different take on the problem with error monads. It's clean, simple, and does not solve the entire problem. So, it also has exceptions, and they are even worse there than at the imperative languages, because everything is high order. I think people are entirely justified in ignoring the problem and moving on. Unless you want to dedicate yourself into research, there is not a clear path to solve it.
- delhanty 10y ago+1 for this! Unchecked exceptions subvert the type system, so they are convenient because they are cheating when compared with Result<T,E> types. Result<T,E> types are the one feature that have me thinking of moving from C++ to Rust. A lot of my experimental C++ code with callbacks looks like a bit like this now: template <OnSuccess, OnFailure> void someFunction(..., OnSuccess onSuccess, OnFailure onFailure) { ... Using the above style: 1. Allows error handling to be type-checked 2. Uses the heap less But it's very clunky! If anybody has a better way to achieve these two things in C++ I'd like to know ... Edit: Erlang, which is known for its reliability, gets this right: kill the whole Erlang process in a clean fashion so that we can get transactional semantics. (It's not that performant though.)
- staticassertion 10y ago> Erlang, which is known for its reliability gets this right: kill the whole Erlang process in a clean fashion so that we can get transactional semantics. (It's not that performant though.) This is also the rust way - panics kill the thread they occur in.
- delhanty 10y agoThanks - that's good to know! Another reason for me to try rust then ...
- deleted 10y ago[deleted]
- fungos 10y agoJust for fun, really, I've done a Result impl in my lab code[1]. I'm willing to improve it just to see how far I can get with it. :) The next improvements will be custom error types (I had it done but reverted in this code) and a error_chain like feature (started testing and forgot to remove some not working code there). [1] https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L43-L81 https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L...
- 10y ago
- mcguire 10y agoDestructors make function returns more expensive; exceptions are faster in these examples because they elide returns. At a guess, destructors would reduce the advantage of exceptions in the case of deep stacks.