[erlang-questions] catch

Lukas Larsson <>
Tue Mar 1 19:01:23 CET 2011


You can match it out in the case, or maybe have a look at try ... catch ... end in the documentation.

case catch(list_to_integer(String)) of
   {'EXIT', {badarg,_}} ->
	io:format("~n~p~n~n",["Arg not Int"]);
    I ->
        do_stuff(I)
end

Lukas
----- Original Message -----
From: "Wes James" <>
To: 
Sent: Tuesday, 1 March, 2011 18:46:34 GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna
Subject: [erlang-questions] catch

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

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:



More information about the erlang-questions mailing list