[erlang-questions] catch

Attila Rajmund Nohl attila.r.nohl@REDACTED
Wed Mar 2 10:07:25 CET 2011


2011/3/1, Wes James <comptekki@REDACTED>:
> I have this escript to create a list of output:
>
> main([String]) ->
> 	case length(String) of
> 		0 -> io:format("~nDid you forget the number?~n~n");
> 		_ ->
> 			case catch(list_to_integer(String)) of
> 				{'EXIT', {badarg,_}} ->
> 					io:format("~n~p~n~n",["Arg not Int"]);
> 				_ ->
> 					I=list_to_integer(String),
> 					Seq=lists:seq(I+1, I+20),
> 					lists:foreach(fun(H) -> io:format("#./runcscript ~p ~n",[H]) end, Seq)
> 			end
> 	end;
> 	
> main(_) ->
> 	io:format("~nDid you forget the number?~n~n").

Haven't tested (or even compiled), but a lot clearer:

main([""]) ->
    io:format("~nDid you forget the number?~n~n");
main([String]) ->
    try
        I=list_to_integer(String),
        Seq=lists:seq(I+1, I+20),
        lists:foreach(fun(H) -> io:format("#./runcscript ~p ~n",[H]) end, Seq)
    catch
        error:{badarg,_} ->
            io:format("~n~p~n~n",["Arg not Int"])
    end;
main(_) ->
    io:format("~nDid you forget the number?~n~n").


More information about the erlang-questions mailing list