Exception propagation?

Mark Scandariato mscandar@REDACTED
Fri Oct 22 21:34:13 CEST 2004


Hi!

Is there a way to propagate exceptions - without changing their semantics (too much) - 
across processes?

For example, in something like:

call(F, infinity) -> F();
call(F, T) -> proxy(F, T).

proxy(F, T) ->
     Tag = make_ref(),
     Proxy = spawn(fun() ->
	Result = try F()
             catch
                 throw:Term -> {'THROWN', Tag, Term}
             end,
         exit({self(), Tag, Result})
     end),
     Mref = erlang:monitor(process, Proxy),
     receive
         {'DOWN',Mref,_,_,{Proxy,Tag,Result}} -> check(Result, Tag);
         {'DOWN',Mref,_,_,Why}                -> exit(Why)
     after
         T ->
             exit(Proxy, kill),
             receive
                 {'DOWN',Mref,_,_,{Proxy, Tag, Result}} -> check(Result, Tag);
                 {'DOWN',Mref,_,_,killed}               -> exit(timeout);
                 {'DOWN',Mref,_,_,Why}                  -> exit(Why)
             after
                 1 -> exit(timeout)
             end
     end.

check({'THROWN', Tag, Term}, Tag) -> throw(Term);
check(R, _) -> R.


The exit(Why) is problematic: it turns errors into exits.  Also, the stack trace 
associated with the original throw (in the bowels of F()) is lost.

I don't think this is terrible, but I'm wondering if it could be improved. It would 
certainly be nice to have the behavior of the timeout vs no-timout cases be as close as 
possible.

Thanks,
Mark.



More information about the erlang-questions mailing list