Erlang处理异常throw和catch的区别

Different between throw and catch in handing exception Erlang

我有一个像下面这样的简单代码来测试 Erlang 如何处理异常。 **** throw 和 catch 参考。

-module(exception).
-export([sum/2]).
-export([is_odd/1]).

sum(A, B) ->
    case is_odd(A) of
    odd ->
        2*A+B;
    Result ->
        Result
    end.


is_odd(A) ->
    case is_integer(A) of
    true ->
        odd;
    _ -> ****({error, "Input error"})
    end.

当我 运行 dialyzer with throw 时,它显示警告:

exception.erl:9: The variable Result can never match since previous clauses completely covered the type 'odd'

可以通过添加

来修复此警告

case catch

功能fun/2。

当我运行 dialyzer with catch时,dialyzer成功通过。

我对接球和投球有点疑惑。我们应该抛出或抓住哪种情况?

如果您使用 throw(对于 ****),您的函数 is_odd(A) 将永远 return 一个正常值:原子 'odd'。这就是 dialyzer 告诉您的内容:第 9 行的子句 (Result ->) 永远无法匹配。如果 is_odd(A) return 是一个值,它必须是 'odd',因此第一个子句将始终被选中。

如果你写'case catch is_odd(A) of ...',那么任何抛出的异常都会被捕获并转化为一个值。在您的例子中,这是元组 {error, "Input error"}。 (请注意,这是一个 "old-style" catch Expression,在所有新代码中通常首选使用现代的 try ... catch ... end。)现在突然又出现了两个可能的值,子句位于line 9也可以被选中,所以dialyzer不会报错

一般来说,对相对罕见的情况使用例外,对常见的事情使用正常的 return 值。但是,有时将异常用作跳远 ("nonlocal return") 以跳出深度递归是个好主意。