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

Steve Vinoski vinoski@REDACTED
Sat May 31 16:20:01 CEST 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140531/20f97615/attachment.htm>


More information about the erlang-questions mailing list