catch link(Pid) when trapping exits

Raimo Niskanen <>
Thu Oct 27 10:26:48 CEST 2005

 (Francesco Cesarini Erlang Training & Consulting) writes:

> In a process trapping exists,
> catch link(Pid)
> of a non existing pid returns true. If we are not trapping exists, the
> return value is {'EXIT', {noproc, .....}}

>From erl -man erlang:

    link(Pid) -> true

               Pid = pid() | port()

          Creates a link between the calling process and  another
          process  (or  port)  Pid,  if  there is not such a link
          already. If a process attempts  to  create  a  link  to
          itself, nothing is done. Returns true.

          Does not fail, but sends an  exit  signal  with  reason
          noproc  to  the  calling process if Pid does not exist.
          This means that, unless the calling process is trapping
          exits (see process_flag/2), it will exit if it tries to
          link to a non-existing process.

It is all there, really. Only it was written before exceptions
were invented as a concept in Erlang, uses the term
"exit signal" in a confusing way and lies about "does not fail".
Ok, it is maybe dead wrong, but the intention was right.
So, what happens is:

link(Pid) -> true           if Pid exists or the caller has
                            process_flag(trap_exit, true).
             error(noproc)  otherwise

The enclosing catch evaluates to {'EXIT',{noproc,Stackdump}}
for error(noproc).             

Weird behaviour, in my opinion..

> Looking into this "feature", my guess is that executing exit(Pid,
> Reason) when trapping exists returns true. I would however expect the
> same behavior in both cases, however....
> 11> catch exit(hello).
> {'EXIT',hello}
> 12> catch exit(self(), hello).
> true
> 13> flush().
> Shell got {'EXIT',<0.44.0>,hello}
> ok
> Is this a bug or a feature?

This is a feature.

exit/1 causes an exception of class 'exit' in the calling process,
       which when enclosed in a catch evaluates to
       {'EXIT',Reason} - no stacktrace since it is of class 'exit'.

exit/2 sends an exit signal to the target process. And if
       the target process (self()) traps exits, it becomes
       a regular message.

> Francesco
> --


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list