[erlang-questions] try/catch

Wes James comptekki@REDACTED
Mon Jun 28 23:24:09 CEST 2010


Thx for responding.  Allan had a similar response.

-wes

On Mon, Jun 28, 2010 at 3:12 PM, Brady McCary <brady.mccary@REDACTED> wrote:
> Wes,
>
> do_query(Sp) ->
>    try
>
>        {ok, Db} = pgsql:connect(?HOST, ?DB, ?USERNAME, ?PASSWORD),
>        {_,[{_,_,Res}]} = pgsql:squery(Db, Sp),
>        pgsql:terminate(Db),
>        {Sp, Res}
>
>    catch
>
>        throw:Term ->
>            Term;
>
>          exit:Reason ->
>            {'EXIT',Reason};
>
>          error:Reason ->
>            {'EXIT',{Reason,erlang:get_stacktrace()}}
>
>    end.
>
> Probably what you want. However, establishing/destorying a connection
> for each query is probably inefficient and using try/catch is not the
> usual erlang idiom of letting supervised processes die fast.
>
> Brady
>
> On Mon, Jun 28, 2010 at 3:39 PM, Wes James <comptekki@REDACTED> wrote:
>> 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
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>
>>
>


More information about the erlang-questions mailing list