catch

Wes James <>
Tue Mar 1 18:46:34 CET 2011


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").
	

I first tried:

case catch(I=list_to_integer(String)) of

But I think there was an error that I was unsafe.

Is there a way to do a variable assignment to retain a good value so
you don't have to do the catch then redo it when it is valid? I.e.,
list_to_ineger will be run twice if it is a valid integer.  Is there a
way to do this once?

thx,

-wes


More information about the erlang-questions mailing list