[erlang-questions] try/catch

Robert Virding <>
Tue Jun 29 00:42:25 CEST 2010


It's sometimes easy to forget that everything returns a value, usually
something useful. Using try ... of as Garret suggested is a good anser
to this problem. It also gives you better control of which errors are
caught.

Robert

On 28 June 2010 23:21, Wes James <> wrote:
> Thx. That worked.  I didn't know try could return a value.  I also
> need to remember that an expression is more than just a single line of
> code but can be several lines separated by a "," in erlang.
>
> -wes
>
> On Mon, Jun 28, 2010 at 3:08 PM, Allan Wegan <> wrote:
> <snip>
>
>>
>> Maybe, the following is what you want:
>>
>> do_query(Sp) ->
>>        {ok, Db} = pgsql:connect(?HOST, ?DB, ?USERNAME, ?PASSWORD),
>>        Result = try
>>                {_,[{_,_,Res}]} = pgsql:squery(Db, Sp),
>>                {Sp, Res}
>>        catch
>>                _:_ -> 'undefined';
>>        end,
>>        pgsql:terminate(Db),
>>        Result
>> .
>>
>
> ________________________________________________________________
> 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