[erlang-questions] gen_event handler question

Robert Raschke <>
Wed Apr 29 10:17:30 CEST 2009


On Tue, Apr 28, 2009 at 10:12 PM, Camille Troillard <> wrote:
> Hello,
> I would like to know if it is possible to write an event handler that would
> receive only certain notifications based on a simple criteria.
> For example, say I have a server process that searches for USB devices, and
> sends notifications whenever new devices are attached or detached.  I would
> like to write a event handler that would register to only certain kind of
> devices (based on an atom describing the device for example) or another
> event handler that would match all devices.
> I have tried to use different function clauses like :
> * in a module :
> handle_event({attach, device_type_A}, State) ->  ...
> handle_event({detach, device_type_A}, State) ->  ...
> * and, in another module :
> handle_event({attach, device_type_B}, State) ->  ...
> handle_event({detach, device_type_B}, State) ->  ...
> And register those two event handlers with the same manager.  Unfortunately
> it fails with a bad_function_clause error.  I was expecting that, but I'm
> running out of ideas.
> Am I overlooking something?
>
> Best Regards,
> Camille
>

You could use a catch all like handle_event(_, State)-> at the end of
your handlers? Not sure if that's the recommended way though.

Robby



More information about the erlang-questions mailing list