views:

67

answers:

2

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
+7  A: 

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.
legoscia
A: 

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)

Olives