开发者

Erlang Error Handling , X unsafe in 'try'

开发者 https://www.devze.com 2023-01-11 13:56 出处:网络
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.

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开发者_JAVA技巧 = [lists:zipwith3(X, Y, Z) || X, Y, Z <-  [Data1, Data2, Data3]],


P = X


The simplest way to fix it is to put the assignment outside of the try-catch:

X =
    try lists:append(lists:zipwith3(fun(X, Y, Z) -> [X, Y, Z] end, Data1, Data2, Data3)) of
        MP -> MP
    catch K -> 0
    end.


It is unsafe I believe due to the fact that you do not cover all the exceptions. When you have

catch K -> (X = 0)

I believe it'll only catch thrown exceptions, there are still errors, and exits. IIRC so you will probably need

catch _:K -> (X=0)

or explicitly catch them as

catch 
  error:K -> (X=0);
  exit:K -> (X=0);
  throw:K -> (X=0)

(I'm not 100% that I have the atom names correct, but the idea is still the same)

0

精彩评论

暂无评论...
验证码 换一张
取 消