[erlang-questions] How to return all records in dets

lloyd@REDACTED lloyd@REDACTED
Sat May 31 16:27:40 CEST 2014


Thanks all, 

You've definitely taken me a step deeper into understanding Erlang.

Best,

Lloyd

-----Original Message-----
From: "Steve Vinoski" <vinoski@REDACTED>
Sent: Saturday, May 31, 2014 10:20am
To: "Scott Lystig Fritchie" <fritchie@REDACTED>
Cc: "erlang questions" <erlang-questions@REDACTED>
Subject: Re: [erlang-questions] How to return all records in dets

_______________________________________________
erlang-questions mailing list
erlang-questions@REDACTED
http://erlang.org/mailman/listinfo/erlang-questions
On Sat, May 31, 2014 at 1:03 AM, Scott Lystig Fritchie <
fritchie@REDACTED> wrote:

> Garrett Smith <g@REDACTED> wrote:
>
> gs> The trick here is to *never* make a mistake in the shell :)
>
> Or, prepend your commands at the shell CLI with "catch".  Something
> like:
>
>     Bad = "yo, I cause pain".
>     {ok, D} = dets:open_file(...).
>     catch dets:lookup(Bad, my_key).
>     catch dets:lookup(D, my_key).
>
> ... would prevent using the bad DETS table name from a cascade that
> kills the shell CLI process than then closes the DETS table so that the
> second lookup() attempt also fails.


Or use the big hammer, catch_exception(true):

1> catch_exception(true).
false
2> Bad = "yo, I cause pain".
"yo, I cause pain"
3> {ok, D} = dets:open_file(...).
{ok,...}
4> dets:lookup(Bad, my_key).
* exception error: bad argument
    in function  dets:lookup/2
       called as dets:lookup("yo, I cause pain",my_key)
5> dets:insert(D, {my_key, foo}).
ok
6> dets:lookup(D, my_key).
[{my_key,foo}]

catch_exception(true) is really handy for these kinds of cases, including
working with sockets in the shell.

--steve





More information about the erlang-questions mailing list