error-handling - 处理异常 Erlang 中 throw 和 catch 的区别

标签 error-handling erlang runtime-error dialyzer

我有一个像下面这样的简单代码来测试 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.

当我用 throw 运行透析器时,它显示警告:

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



可以通过添加来修复此警告

case catch



功能乐趣/2。

当我用 catch 运行透析器时,透析器成功通过。

我想知道 catch 和 throw 。我们应该抛出或捕获哪种情况?

最佳答案

如果您使用 throw(用于 ****),您的函数 is_odd(A) 将只返回一个正常值:原子“奇数”。这就是 dialyzer 告诉你的:第 9 行 (Result ->) 的子句永远不会匹配。如果 is_odd(A) 返回一个值,它必须是 'odd',所以总是选择第一个子句。

如果你写'case catch is_odd(A) of ...',那么任何抛出的异常都会被捕获并转化为一个值。在您的情况下,这就是元组 {error, "Input error"}。 (请注意,这是一个“旧式” catch Expression ,并且通常更喜欢在所有新代码中使用现代 try ... catch ... end 。)现在突然又出现了两个可能的值,第 9 行的子句也可以被选中,所以透析器不会提示。

一般来说,对于相对罕见的情况使用异常,对于常见的事情使用正常的返回值。但是,有时将异常用作跳远(“非本地返回”)以摆脱深度递归可能是个好主意。

关于error-handling - 处理异常 Erlang 中 throw 和 catch 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54770193/

相关文章:

运行暗网检测的 OpenCV 未知层类型

ruby - 使用 Eventmachine Http Request 时获取有关错误的信息

网页仅在点击刷新时出现

php - Laravel 4 - 自定义错误报告

ssl - TLS - 通常在哪里为 MQTT 代理执行 tls 握手?

erlang - 区分字符串和 Erlang 中的列表

jquery - 非描述性jQuery Ajax错误

math - 在Erlang中查找子集,请帮我优化解决方案

安卓模拟器崩溃: "Dx bad class file magic"/ClassNotFoundException on startup?

io - 在 Perl 6 REPL 中使用 chdir 的奇怪(内部?)默认构造函数错误消息