In C and C++ you can return a single variable inside a function. Now in the case that variable is returning data, and not an error code, you can use exceptions. But how is that possible? If you data being returned is the data you tell your function to return, then there is practically no way for the function calling it to know it’s success (because the return is used for data, and not an error code). Also, the ability for a exception to literally stop the function in the middle of the code to return the error. How do C++ compilers achieve this?
3
When a function throws an exception it does not return a value of the type specified by the function specification, and it doesn’t return to the point where the function was called. It instead “returns” an exception object, and it “returns” that object to whatever catch
block that catches that particular kind of exception.
That I put “returns” in quotes is intentional. It’s best not to think of throw
as returning a value. It instead throws a value to the catch
block that handles the exception. The mechanism for handling normal function call and return is almost always implemented in the form of a call stack.
Exception handling works by a different mechanism than the call stack, but that exception handling mechanism has to do so in a manner consistent with the call stack. The catch block that catches a thrown exception might be several calls above the function that called the function that threw the exception. All of the intervening stuff, the temporary variables passed as arguments to and the local variables declared in those intervening functions, has to be cleaned up along the way. This is called “unwinding the stack”.
1
There are several misconceptions underpinning your question.
First of all, the distinction you make between returning data or an error code is an artificial distinction. The C and C++ languages do not have that distinction. To them you are always returning data. It is only us programmers that attach the meaning of an error code to some of that data.
Secondly, when you leave a function, there is no requirement that you end up back at the point where the function was called. You can end up in some completely different location of the code.
Now, exceptions can be implemented in various ways. I will outline one of those ways. It is not the most efficient one, but it is one of the easiest to understand.
First I will explain how a regular function call works. When you call a function in your C or C++ code, the compiler generates instructions to take several steps. First the (values of) the arguments to the function are pushed onto the stack. Then the address is pushed where to continue processing after the function returns (with a normal return). And lastly, a jump is executed to the first instruction of the function.
For the exception handling, the compiler generates some additional information that gets placed on the stack along with the local variables. This information tells the throw
statement how to clean up those local variables and where the catch
blocks are located.
When an exception is thrown, the throw
statement walks over the stack to search for an appropriate catch
block, cleaning up the local variables that it finds along the way. When an appropriate catch
block has been found, a jump is performed to continue execution at that point.
As you can see, returning from a function and throwing an exception use completely different information on where to continue execution after the return/throw. For that reason, if a function does not indicate errors with an error code, but uses exceptions, you can safely presume that the function succeeded if it returns from its call. If the function had thrown, the code immediately after the function call would not have been executed.
2