Originally Posted by Mario F.
. Consider a function that returns an int and that implements error handling through negative values. Lets not even assume we would need those negative values for something else. Basically lets pretend the function only expects to return positive values and that negative values are error codes... For the sake of simplicity let's assume there's only one error code. -1.
I'm hoping by now you are already imagining the mess your are getting yourself into. Everytime you call that function you have to check its return value and probably call another error handling function if the return value is -1. Everytime you don't, you are not checking for errors. Hopefully the error handling function will put the program back into a valid state. But what if -1 is a unrecoverable error code and the program needs to exit? Well, everytime you call that function, you cannot simply call a generic "exit" function. You will need to deallocate memory and do any other necessary cleaning. This will be different depending on where the function is called.
But... let's still stick to the good ol' ways and pretend on this case the error was no severe. A simple error hadling function would solve the error. Nice, isn't it?... Well, what if after some time you just felt the need to add another error code. You just discovered another type of error that could occur. So now you have errors -1 and -2. So... back to code and change every piece of code that calls your function and checks for errors.