Non-blocking handling of exceptions in event handlers

1 view
Skip to first unread message

Uri Goldstein

unread,
Nov 17, 2009, 11:37:42 AM11/17/09
to altnetisrael
Hi Guys,

A technical question, if you will. Take a look at this code:

if (OnDataReceived != null)
{
OnDataReceived(data);
}

Assuming the OnDataReceived event has multiple event handlers assigned
to it, the first handler to throw an exception will stop the execution
change and prevent the execution of any handlers following it.

Do you know of a pattern to circumvent this?

I was thinking of something along these lines:

Delegate[] handlers = OnQuoteReceived.GetInvocationList();
foreach (Action<data> handler in handlers)
{
try
{
handler(data);
}
catch (Exception ex)
{
// log that an exception occurred and move on with your life man!
}
}

Anyone up for a code review? :)


Thanks,
Uri

Oren Ellenbogen

unread,
Nov 17, 2009, 12:30:45 PM11/17/09
to altnet...@googlegroups.com
This is the right pattern (I remember Juval wrote an helper class to do it in a generic manner)

Another thing you can do is to set the "add" of the event and wrap it internally with anonymous method to protect the call.
Well, this is a stupid idea but fun to think about, so I'll let you figure out why (it is stupid).
Here it is (wrote it in gmail, no compiler):

private event OnDataReceived _dataRecievedEvent = () => {}; // prevent the tedious/dangerous if (_dataRecievedEvent != null)
public event OnDataReceived OnDataReceived
{
  add {
    _dataRecievedEvent += ()=> { try { value(); } catch(Exception e) { _logger.Error(e); } }; // wrap original caller with my protected code.
   }
   remove { 
      _dataRecievedEvent -= value; // hmmm... would it work... ?
   } 
}

cheers,
Oren.


--

You received this message because you are subscribed to the Google Groups "altnetisrael" group.
To post to this group, send email to altnet...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/altnetisrael?hl=.





--
Cheers,
Oren.

:: Visit my blog: http://www.lnbogen.com/

Robi Yagel

unread,
Nov 17, 2009, 2:59:07 PM11/17/09
to altnet...@googlegroups.com
Juval has a whole discussion on this pattern in his book .net components

Avishay Lavie

unread,
Nov 17, 2009, 3:30:02 PM11/17/09
to altnet...@googlegroups.com
Not really adding anything to the discussion (I agree with Oren above), but I like how here at the Alt.NET list we're all on a first-name basis with Mr. Lowy. :)

Ariel Raunstien

unread,
Nov 18, 2009, 1:46:40 AM11/18/09
to altnet...@googlegroups.com
Well, Oren, naturally it won't work because you wrote it in gmail.
AFAIK, email is not being jitted.

You should ask tougher questions.

On Tue, Nov 17, 2009 at 7:30 PM, Oren Ellenbogen <oren.el...@gmail.com> wrote:

Uri Goldstein

unread,
Nov 18, 2009, 5:12:42 AM11/18/09
to altnetisrael
Thanks for your help guys! Much appreciated.

And thanks for the "add pattern" - it looks very elegant.

It's funny you guys mention Juval Lowy's book on .net components as I
have it by my bed and I'm reading it 2 pages at a time. Should be into
that section by spring of 2011 ;)


Thanks again!
Uri

PS - I think what Avishay is hinting at is that we need to start
addressing him as Mr. Lavie from now on :D

On Nov 17, 7:30 pm, Oren Ellenbogen <oren.ellenbo...@gmail.com> wrote:
> This is the right pattern (I remember Juval wrote an helper class to do it
> in a generic manner)
>
> Another thing you can do is to set the "add" of the event and wrap it
> internally with anonymous method to protect the call.
> Well, this is a stupid idea but fun to think about, so I'll let you figure
> out why (it is stupid).
> Here it is (wrote it in gmail, no compiler):
>
> private event OnDataReceived _dataRecievedEvent = () => {}; // prevent the
> tedious/dangerous if (_dataRecievedEvent != null)
> public event OnDataReceived OnDataReceived
> {
>   add {
>     _dataRecievedEvent += ()=> { try { value(); } catch(Exception e) {
> _logger.Error(e); } }; // wrap original caller with my protected code.
>    }
>    remove {
>       _dataRecievedEvent -= value; // hmmm... would it work... ?
>    }
>
> }
>
> cheers,
> Oren.
>

Uri Goldstein

unread,
Nov 18, 2009, 5:18:56 AM11/18/09
to altnetisrael
Wait! Let's take this discussion to another place.

In my sample code, have you noticed that "handlers" is an array of
Delegate, but that "handler" is of type Action<data> ? Can anyone
explain to me how I managed to pull this cast off?

And why would it be possible for me to compile with Action<int> which
will probably lead me to explode in run-time?

In broader terms - what's the relationship between Delegate and
Action?

Cheers
Uri
Reply all
Reply to author
Forward
0 new messages