catching errors from linked processes: simplest way?

Raimo Niskanen raimo.niskanen@REDACTED
Wed Apr 23 08:34:48 CEST 2003

You could add some supervision to the client by using a toplevel catch. 
Then you will get a stack dump. The following code also demonstrates a 
line number macro trick with the process dictionary that is possible if 
you have a toplevel catch in the client.

Runtime result:
21> test:top().
           {shell,eval_loop,2}]}} at test:22

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


-define(line, put(location,{?MODULE,?LINE}),).

top() ->
	case catch do() of
	    {'EXIT',_} = Exit ->
		{Module,Line} = get(location),
		io:format("~p at ~w:~w~n", [Exit,Module,Line]),
	    Result ->

do() ->
     ?line foo(),
     ?line bar().

foo() ->
     ?line a=b.

bar() ->

Chris Pressey wrote:
> On Tue, 22 Apr 2003 15:35:16 -0500
> Chris Pressey <cpressey@REDACTED> wrote:
>>If I do the translation in the client, I would have:
>>  foo(A, B) ->
>>    case A of
>>      true ->
>>        bar(B);
>>      false ->
>>        throw(only_applies_under_condition_a)
>>    end.
>>which is hardly any improvement over the original.
> Ah!  I think I see a compromise: notify the supervisor of the nature of
> any potential upcoming errors.  So the code becomes something like:
>   foo(A, B) ->
>     supervisor:potential_error(only_applies_under_condition_a, [A,B]),
>     A = true, bar(B).
> where supervisor:potential_error/2 sends a message to the supervisor,
> which stores it in a dictionary using the pid the message came from as
> the key, and which retrieves it when an error actually does occur.
> The code is still fairly easy to read, and the supervisor doesn't need
> to know about the structure of the code it's supervising, yet it can
> know some details about what state the process was in when it crashed. 
> Sweet!
> -Chris

More information about the erlang-questions mailing list