[erlang-questions] Exceptions vs Tagged Values
Samuel Rivas
samuelrivas@REDACTED
Thu Nov 9 08:41:15 CET 2006
Robert Virding wrote:
> In many cases you need to differentiate this. If file:open bombs it
> would be very nice to know if I couldn't open the file or if my
> arguments were wrong. That is not the same thing and handling them as
> such will not make users happy.
I suppose that is why try does not catch errors unless you force it to
do so.
> There will be written an aweful lot of wrapper functions to help you
> remember:
>
> file_open(Name, Opts) ->
> try file:crashing_open(Name, Opts) of
> Pid -> {ok,Pid}
> catch
> error:not_found -> {error,not_found};
> error:permission -> {error,permission};
>
> <all other reasons why a file could not be opened/created>
> end.
I do not get that point:
file_open_tagged_version(Name, Opts) ->
try file_open_throwing_version(Name, Opts) of
Pid -> {ok, Pid}
catch
Reason -> {error, Reason}
end.
You need not know, nor write, all the possible Reasons. Naturally,
there is the opposite version:
file_open_throwing_version(Name, Opts) ->
case file_open_tagged_version(Name, Opts) of
{ok, Pid} -> Pid;
{error, Reason} -> throw(Reason)
end.
I do not see why throwing (or exiting, or erroring) is worse in terms
of documentation for error cases.
> BUT THEN AGAIN - it would simplify writing many of the libraries. For
> example in io when scanning/parsing a file there would be no need to try
> and recover on an error to keep going - just BANG exception and let the
> some other process handle it. Epp detects an error - BANG exception.
> Hell we could do it for the compiler as well. Why go to all the trouble
> to keep track of errors and warnings and return something helpful? It
> detects an error - BANG exception and let the user handle it. Seeing we
> have removed the grey area between right and exception warnings have to
> go - BANG exception. The compiler would return ModuleName |
> {ModuleName,Bin} (untagged) depending on the arguments, and the
> exceptions {'EXIT',wrong} and {'EXIT',unused_variable}.
Here you are talking about applications, not about functions. In both
tagged and throwing approaches error handling is still needed somewhere
in the application.
Regards
--
Samuel
More information about the erlang-questions
mailing list