"catch" for "throw" without ":"?

Why does this work well:

% (1)
try
    throw({abcd456, {aaa, "Name666"}})
catch
    %% no need for  throw:{abcd456, {aaa, FieldName}} ->

    {abcd456, {aaa, FieldName}} ->
        io:format("Caught an error: ~p~n", [FieldName])
end.

===>
Caught an error: "Name666"
ok

whereas this does require “type:<matched_error_info>” syntax, as it must:

% (2)
try
    error({abcd456, {aaa, "Name777"}})
catch
    error:{abcd456, {aaa, FieldName}} ->
        io:format("Caught an error: ~p~n", [FieldName])
end.

?

In other words, why does the (1) work without “:” ? Isn’t this in odds with the documentation?

Informal answer: the default error class caught by catch clause is throw. catch A -> ... end is essentially equivalent to catch throw:A -> ... end. Formal answer: Expressions — Erlang System Documentation v27.3 this paragraph mentions this shortcut.

4 Likes