[eeps] EEP 049: Value-Based Error Handling Mechanisms

José Valim jose.valim@REDACTED
Thu Oct 22 17:44:18 CEST 2020


> I don't know if we have a problem with introducing 'else' as a keyword.
> Will that potentially break old existing code?
>

If a new keyword is a contention point, "catch" can be one option:

change_and_backup(File) ->
  begin
    {ok, Value} <~ file:read_file(File),
    NewValue = do_something_with_value(Value),
    ok <~ file:write_file(File ++ ".backup", Value),
    ok <~ file:write_file(File, NewValue),
    NewValue
  catch
    {error, Reason} -> erlang:error({backup_error, Reason}, [File])
  end.

Use begin/catch for local/functional returns and try/catch for
non-local/imperative returns.  It may even make it clearer they are two
approaches for tackling the same problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/eeps/attachments/20201022/70ee9fd3/attachment.htm>


More information about the eeps mailing list