Let it crash?

Vance Shipley vances@REDACTED
Thu Aug 19 17:35:43 CEST 2004


On Thu, Aug 19, 2004 at 11:31:33AM -0300, Eduardo Figoli wrote:
}  
}  My question is when should I use catch in my code or set trap_exit 
}  flag in true?


Here's an example from work I'm doing right now.  I have an API 
module foo.erl and a server process implemented in foo_server.erl.
In foo.erl there are no catches, it just crashes, the user's process
will exit (unless she's running in a catch).  In foo_server.erl when
it parses external data it does so in a catch and handles errors by
reporting and discarding.  If it did otherwise the process would crash
and although it would be restarted (through supervision) the state
data would be lost causing problems for many other users.  But even
here foo_server.erl can still crash if a programatic error occurs.

     case decode(Value) of
          Rec when is_record(Rec, foo) ->
               ...
          {'EXIT', Reason} ->
               ...
     end

We expect the decode function to either fail or return a valid #foo{}.
If it does not it's a programming error.  We can have bad data, because
it didn't come from one of our own programs, it is external.  We do
expect our own program (decode/1) to behave as expected.  if the data
were originated in one of our programs we would expect it to be correct.

The definitive text on this is Joe's thesis:

	http://www.sics.se/~joe/thesis/armstrong_thesis_2003.pdf

As for trap_exit you should set it in gen_[fsm|server] code if you 
need to do cleanup before exiting.  Without setting it your process
will just end where it is.  By setting it to true you will get a
call to handle_info/3 with an event of {'EXIT', Reason}.  Here you
can clean up after by for instance removing entries in ets or writing
out a log entry.

	-Vance




More information about the erlang-questions mailing list