4 ms·
Perhaps naive question: is there a language where errors are automatically returned from a function (in lieu of exceptions)? So if you have a function x that h
by coldtea 12y ago
Perhaps naive question: is there a language where errors are automatically returned from a function (in lieu of exceptions)?
So if you have a function x that has some unhandled "exception" thrown, what it does is it returns an error value at that point to the caller (instead of actually throwing an Exception).
Does that exist?
- tel 12y agoI believe you can't have that happen universally since then you don't get the benefit of having exceptions bubble at all—you're just de facto operating in an error monad. On the other hand, if it's not universal then you have to set boundaries where the exception to value transition occurs and this is "just" a catch block.
- coldtea 12y ago>I believe you can't have that happen universally since then you don't get the benefit of having exceptions bubble at all—you're just de facto operating in an error monad. I was thinking something like you get an Optional, and you check it. If you use it without a check (e.g. something like "try" in Rust), and it's an error value, then it "explodes" again and bubbles up.
- tel 12y agoThe mechanics of that are easily captured with a monad, but baking it into the natural semantics of the language would be a neat experiment, I agree.
- tatterdemalion 12y agoThis is what Rust does. Not to try to summarize the article, but functions which could 'throw exceptions' instead of returning type T, return type Result<T, E>, where E is an Error type. The try!() macro wraps an expression that could be an error, returning early with the error value if it is an error and evaluating to the non-error value if it is not an error. EDIT: Oh, I see, you mean without having to declare it.
- barrkel 12y agoI don't understand how what you suggest is distinct from exceptions as we already know them. Consider function f() which calls function g() which calls function h(). So the call stack is f -> g -> h. Suppose that h() returns to g() with an error condition. Per your description, it now returns to f() with the error condition, rather than continuing. f() in turn returns with the error condition, because the function it called had an error. The only way I can read your suggestion is a distinction between errors caused during a subroutine call, and errors caused directly in the body of the current function (like a divide by zero or something). But these errors are quite rare; most code is composed of calls to other code. So I'm not sure what error you could have that causes immediate return which is also distinct from a the error-case for return value of the function.
- coldtea 12y ago>I don't understand how what you suggest is distinct from exceptions as we already know them. In the handling, something you catch vs something you check the return value for (e.g. with an Optional). So, it's like an (implicit) try/catch block, where the catch just returns an error value.
- cpeterso 12y agoThe Icon programming language (from 1977) has something like this. Functions have an implicit "success" or "failure" return value in addition to regular return values. The success/failure status can be used in control flow or bubbled up the stack. https://en.wikipedia.org/wiki/Icon_%28programming_language%29#Goal-directed_execution https://en.wikipedia.org/wiki/Icon_%28programming_language%2...