Legacy error handling tends to follow the method that all functions return a code depending on success/failure. You would check this code and handle (if an error) appropriately.
Modern programming languages however follow the exception model, where if something exceptional happens that cannot be handled properly an exception gets thrown - which keeps bubbling up until its handled.
My question is why have we moved towards the exception model? What are the reasons behind this? Why is it better?
Explanations/links would be much appreciated.
Return codes are more brittle The error is ignored when "returned", and will possibly explode later (i.e. a NULL pointer). The same problem won't happen with exception. The error won't be ignored.
An application that uses exceptions is more robust than an application that uses return codes. An application that uses exceptions can also give the cleanest code, since return codes don't have to be checked after every call.
Error codes are safer for well-reviewed code Error codes mean that you must carefully look at function calls to see if the programmer handled the possible errors. Exceptions mean that you must imagine what happens if an exception is thrown anywhere in the flow.
The general guideline is that your application should not throw more than 1000 exceptions a second. When you throw exceptions for exceptional scenario's performance will in most cases not be a problem. @Jodrell: Exactly! Error codes are useful in languages that don't have exception handling.
I've written about this at length: Exceptions vs. status returns, but briefly:
To expand on the last point: if you forget to do what you should be doing with status returns, you ignore errors. If you forget to do what you should be doing with exceptions, the exception bubbles up to the outer layer of the software where it becomes visible.
Here are a couple of reasons
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With