Reading @MRonErlang’s #book, and watching people struggle once more trying to explain catch and throw in Erlang… I came up with an idea
…
Should we eventually true-up catch&throw?
The Problem
catch and its friend throw, were created to implement non-local return. That is, returning from a function before it’s done with its evaluation.
@MononcQc has probably the best explanation of it in LYSE, but the fact that he (and all of us trainers) needs to spend so much time teaching something that should be far simpler, it’s a symptom of the issue in general.
The Root Cause
I personally believe that, while throws, errors and exits do have some things in common, conflating them into a single semantic category never really benefited any Erlang programmer. And it was surely detrimental to those (like myself) who came from other languages where throw was clearly used to throw exceptions, not possible return values. For that, I expected to find something along the lines of return (or ^, in Smalltalk style).
The Proposed Solution
The Brutal Version
- Rename
throwasreturn. - Rename
catchashandle_return(or something cleverer). - Stop handling errors with
handle_return. The idea would be thathandle_returnwill only handle… well… values returned fromreturn. - Remove the ability to handle
throws fromtry…catch…after…end. Or, alternatively, handle them as results, not as exceptions.
The More Amicable Version
- Add a new keyword
returnthat works exactly likethrow. - Add a new keyword
handle_return(or something cleverer) that works likecatchbut it doesn’t turn errors or exits into tuples. - Do not adjust
try…catch…after…endto handle these new returns within thecatchpart.- If we feel like we need to handle them somehow, make
handle_returnimplicit intry(i.e. as if everytry Expr…would betry handle_return Expr, thus returning the results as values).
- If we feel like we need to handle them somehow, make
- Slowly deprecate
catchandthrowover several OTP versions, recommending their replacement with the new keywords.- Or just deprecate
catchmaking it evident thatthrowdoes throw actual exceptions that can only be captured within atry…catch…block.
- Or just deprecate