When we run into a "live" mistake in C programing we use the "Return Value" mechanism in order to take care of it. In C++ we use a different mechanism – the Exceptions mechanism. Why did C++ implemented a different mechanism? What are the differences? All the answers can be found in this article.
First it is important to understand that both mechanisms were meant to take care of a "living mistake". Meaning a mistake that occurs during run-time and is not a bug! There was no way we could just change the design of the code or the implementation methodand prevent this mistake from the beginning (because if there was such a way we should have done that, and this mistake is actually a bug).
The mechanisms are meant to take care of mistakes such as "out of system resources" (out of memory, no such file, etc.), wrong parameter value inside a method / function (dividing by zero, out of array index, etc.), etc.
What makes Exceptions better?
- The most important thing about Exceptions vs. Return value is that Exceptions forces someone in the program to take responsibility to the problem! Unlike the Return Value mechanism that can just be ignored, Exceptions stop the regular execution of the code, and it will not restore it till the exception will be caught and fixed.
- The Returned Value mechanism is using some values of the returned value type in order to mark mistakes. However, this kind of marking is not always clear. Think about a return value "int", when the negative values mark mistakes. What does "-1" stand for? What does "-2" stand for? This mechanism forces the method / function to create a table that relates each mistake to a specific value. And even worse, it forces everyone that wants to use this method / function to know this table as well.
In some cases situation is even worse, since we just do not have free values that can be used
a) We do not have enough spare return values for all possible mistakes (i.e. Boolean return value).
b) All returned values are legit return values and there is no illegal values that can be used to mark mistakes.
c) The method had no return value (constructors and destructors).
- Usually people do not check the return value of functions that are not supposed to return a value, this way they might not notice or ignore the knowledge about the mistakes. So with the Returned Value mechanism not only they must check the return value, they also must do it in each function separately. In Exceptions, on the other hand, we can call several functions and surround all of them with one big "try" block!
- With the Return value mechanism we must know how to treat the error the moment we return from the method. This demands us to make sure that all parts of the code know how to handle every possible error. It also creates a "spaghetti code" an unreadable code that combines different functions together and makes it hard to understand what it does. With Exceptions the code that responsible for fixing the mistake is separated then the "normal executing" code and is writing only in one place – in the "catch" block.
You might also be interested in the following articles: