Erlang 错误处理,“try”中的 X 不安全
谁能告诉我为什么这段代码会说 X 在“try”中不安全,我知道为什么,但更知道如何修复它。
try X = lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of
MP -> X
catch K -> (X = 0)
end.
%MP = [lists:zipwith3(X, Y, Z) || X, Y, Z <- [Data1, Data2, Data3]],
P = X
Can anyone enlighten me as to why this bit of code spits back that the X is unsafe in 'try', well I know why, but more so how to fix it.
try X = lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of
MP -> X
catch K -> (X = 0)
end.
%MP = [lists:zipwith3(X, Y, Z) || X, Y, Z <- [Data1, Data2, Data3]],
P = X
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决这个问题的最简单方法是将赋值放在 try-catch 之外:
至于为什么会发生这种情况,Erlang 参考手册中有关表达式的章节 说:
它曾经说过“这有待改进”,但在 此提交。
The simplest way to fix it is to put the assignment outside of the try-catch:
As for why this happens, the chapter on expressions in the Erlang reference manual says:
It used to say "This is to be improved", but that was removed in this commit.
我认为这是不安全的,因为您没有涵盖所有例外情况。
当你有
我相信它只会捕获抛出的异常时,仍然存在错误并退出。国际研究委员会
所以你可能需要
或明确地将它们捕获为
(我不是100%我的原子名称是正确的,但想法仍然是一样的)
It is unsafe I believe due to the fact that you do not cover all the exceptions.
When you have
I believe it'll only catch thrown exceptions, there are still errors, and exits. IIRC
so you will probably need
or explicitly catch them as
(I'm not 100% that I have the atom names correct, but the idea is still the same)