try/catch

Wes James <>
Mon Jun 28 22:39:58 CEST 2010


So in a previous post, it was mentioned that a variable in a try can
not be used afterwards.  How is this done then?  Like this:

do_query(Sp) ->
	{ok, Db} = pgsql:connect(?HOST, ?DB, ?USERNAME, ?PASSWORD),
	try {_,[{_,_,Res}]}=pgsql:squery(Db, Sp)
	catch
    	throw:Term -> Term;
    	exit:Reason -> {'EXIT',Reason};
    	error:Reason -> {'EXIT',{Reason,erlang:get_stacktrace()}}
	end,
	pgsql:terminate(Db),
	{Sp,""}.


I want to call squery and return a value to Res, but if there is an
error return nothing.  {Sp. ""} was {Sp, Res}  but doesn't work since
Res is in the Try.

thx,

-wes


More information about the erlang-questions mailing list