2 ms·
Exceptions are not the only alternative to error codes. Many early programming languages handled errors by providing multiple return addresses to functions. T
by adrian_b 6mo ago
Exceptions are not the only alternative to error codes.
Many early programming languages handled errors by providing multiple return addresses to functions.
Thus a function returned to the place of invocation in the normal case, otherwise it returned to one of the error handlers that were grouped at the end of the function that invoked it, away from the happy path.
This was more efficient than the modern way of returning an error code, as it eliminated the superfluous testing of the error code and the expensive conditional jump based on the result of the testing.
Moreover, in my opinion in the majority of the cases maximum information about an error is inside the function that has invoked the function where the error happened and neither in an exception handler several levels above it, nor inside the invoked function, so the place where the error handlers had to be put in this old method is actually optimal for meaningful error messages.
From the point of view of the source text, this old error handling method is equivalent with using exceptions with the constraint of placing the exception handlers in the function that has invoked the function that generates the exception. This limitation enabled a more efficient implementation than for exceptions where the handler can be placed anywhere above the invoked function and a complex stack unwinding may be necessary.
- sieve 6mo agoWhile it sounds interesting, I do not see this gaining traction. My main complaint would be that I do not want to think about errors except when I want to. This system sounds like Java checked exceptions in a way. You have to handle every exception that a function might throw every single time. That eventually resulted in the famous catch-swallow stratagem. I distinguish between Faults and Errors. Faults are system level events you cannot recover from. Errors are things you may encounter in the course of doing business. Most applications do not really need to do any recovery; simply tell the user something bad happened and ask them to fix the problem. For workflows that do need error recovery, or functions with logical failure conditions, you are free to use a Result or Optional type. Also, I have implemented exception handling with stack unwinding for my VM. The code that does it is not really that complex. If you want to retry... then things get messy.