views:

234

answers:

5

As a general rule, are there ever any circumstances in which it's acceptable for a method responsible for listening to an event to throw an exception (or allow to be thrown) that the class raising the event will have to handle?

Given that such an exception would stop other listeners to that event from being called subsequently, it seems a bit 'antisocial' to allow this to happen, but on the other hand, if there is an exception, what should it do?

+1  A: 

In an ideal world, event handlers shouldn't raise exceptions. Raising an exception in an event handler tends to lead to very difficult to handle situations, and unexpected behavior. As you mentioned- this blocks subsequent event handlers from seeing the event, and the exception propagates into the event producer's code since it raised the event.

Ideally, event handlers should be fast (if they're long running, they should try to schedule the work in a separate thread) and as close to error free as possible.

Reed Copsey
"Ideally, event handlers should be fast" - what about Page.Load in an ASP.NET app? There are no general rules here. Propagating an exception from an event handler is perfectly OK, and generally leads to the easy-to-handle situation of an exception propagating to the top level handler.
Joe
+4  A: 

Throwing an exception from a event handler is in many ways similar to throwing an exception from a IDisposable.Dispose method (or a C++ destructor). Doing so creates havoc for your caller because you leave them with little option.

  1. Ignore the exception and let it propagate. This breaks their contract to inform all listeners of an event. This is a very real problem if anyone above them on the stack catches the exception.
  2. Catch it call the other handlers and rethrow. But what happens if one of the others throw as well?
  3. Swallow the exception. This is just bad in general. Event sources should have no knowledge of their caller and hence can't know what they're swallowing.
  4. Crash the process because you're toast.

Of all of these #4 is the best option. But this is rarely done and can't be counted on.

I think in your component you really only have a few options

  • You are calling the code which is throwing and are in the best position to handle the exception. If it's not handleable by you then it's unreasonable to expect it to be handled by anyone else. Hence crash the process and be done with it.
  • Don't call the API which throws
JaredPar
"This breaks their contract to inform all listeners" - disagree. There is no contract to inform all listeners if an exception is thrown."Catch it call the other handlers and rethrow." - how? .NET events don't provide you with a list of "other handlers"."Crash the process..." - it doesn't crash the process, the exception will be propagated to the code that raised the event, and will be handled like any other exception. The process will only crash if there is no top level handler, again like any other exception.
Joe
@Joe the contract for an event is that all listeners will be notified. Failing to notify them is a violation of that contract. What good is the `Button.Click` event if I can't depend on my handler actually executing when the button is clicked?
JaredPar
@Joe (cont) in this case having a top level handler is the worst thing that can happen. Throwing in an event handler is a violation of the contract of the vast majority of events and causes broken assumptions in other components. This will almost certainly lead to future errors and / or exceptions in those components. These failures are far from the original real failure and hence much harder to track down. Fail fast and fail loud.
JaredPar
@JaredPar - I disagree that there is any such contract when an exception is thrown. An exception could be thrown in an overridden OnClick method before *any* handler is executed - in which case *no* handler is notified. Of course you may have specific situations where you define such a contract in your own app, but it's not true for events in general. Are you saying an ASP.NET Page.Load event handler that doesn't swallow exceptions is breaking a contract?
Joe
@JaredPar - "having a top level handler is the worst thing that can happen" - but that's exactly what happens in an ASP.NET app if a Page.Load handler throws. Whether there should be a top-level handler or fail-fast is orthogonal to this question - the situation is exactly the same whether the exception is thrown from an event handler or from any other code.
Joe
"Throwing in an event handler is a violation of the contract of the vast majority of events and causes broken assumptions in other components" - examples would help to understand what you mean here. And why it represents the "vast majority" of cases.
Joe
@Joe The standard example is if you know that 2 events must be ordered or paired. Take for example a transaction Opened / Closed pair. It's reasonable to write code against the opening event coming before the closed event. However that breaks down if someone ahead of you throws during the Opened, the exception is caught above and the transaction is later closed. At this point I receive a close before an open and my assumptions are violated and leads to an error in my code. In this case the exception is not caught, a bug report is filed and investigation starts with me.
JaredPar
@Joe (cont) it's reasonable to say the blame lies in the Transaction for not ensuring all of it's contracts were met in the face of an exception. But how could it do so? It has now sent an Open sucessfully to a fraction of it's listeners. It still owes them a Close (but not the rest). It could try to raise the Close event for only those who saw the Open but what if one of them throws? Now suddenly it has 2 exceptions and a whole bucket of lot of bad state to deal with.
JaredPar
@JaredPar - sound a bit contrived, and an odd way to implement transactions. I agree that you can design specific cases where your contract requires event handlers to swallow exceptions, even if I think that such a design is fragile. What I disagree with is that such a contract exists in the general case, and certainly not for the "vast majority" of events in the .NET Framework.
Joe
@JaredPar - Although I'm not disputing it, indeed I think it's a damn good thing, but I've never heard of this contract before- is it documented anywhere from Microsoft?
Flynn1179
@Joe not contrived at all. That's a real world bug I've encountered and we spent quite some time working out the fix. I disagree about the contract not existing in the vast majority of events. I counter with the question "For what event is it acceptable to not notify all handlers when the event occurs?". If this is acceptable reliable programs cannot be created.
JaredPar
@JaredPar. ASP.NET Page.Load would be one event for which it's acceptable to not notify all handlers (but only in the case of an exception of course). I disagree with the assertion that this means reliable programs cannot be created, because this would mean that any program with event handlers that don't swallow exceptions was unreliable. And I think almost every .NET app I've ever seen has at least one event handler that doesn't swallow exceptions. But I think we will just have to agree to differ on this...
Joe
@Joe, to clarify. I don't think many (if any) .Net event handlers swallow exceptions (that would be a bad thing). They have an implicit dependency on their handlers not throwing.
JaredPar
+1  A: 

In an ideal word, exceptions should not exist. In a developer real word, exceptions are always expected and should be intercepted (caught), propagated or inhibited as situation asks.

So

are there ever any circumstances in which it's acceptable for a method responsible for listening to an event to throw an exception

Yes. You can expect an exception from every method, responsible or not to an event.

To catch almost every exception from a Windows application use:
AppDomain.CurrentDomain.UnhandledException
Application.ThreadException

serhio
+1  A: 

The only two types of exceptions that should come out of events are serious, potentially process-ending ones like System.OutOfMemoryException or System.DllNotFoundException, and things that are clearly programming errors, like System.StackOverflowException or System.InvalidCastException. Catching and dropping these kinds of exceptions is never a good idea -- let them float up to the top and let the developer decide what to do with them on an application level.

As for the rest... any common or garden-variety exception like System.IO.IOException should be handled inside your event, and you should have some mechanism for returning such error conditions to the caller.

Warren
Oo, that just got me thinking.. what about `ThreadAbortException`?
Flynn1179
`ThreadAbortException` is an interesting one because it doesn't matter if you catch it or not -- it'll just get rethrown at the end of a catch block.
Warren
-1: Exceptions should be only handled inside the event (or elsewhere) if you know what to do with them. And in the general case (e.g. inside a handler for one of the events in the Framework - Page.Load, Button.Click etc) there is no mechanism for returning error conditions to the caller.
Joe
Ummmm, Joe? You, the app developer, aren't typically the caller of Page.Load or Button.Click so what difference does it make if you can pass back error conditions in those cases? I was talking about cases where you design your own events. And in either case, it's still as I said: Handle what you can handle, and let truly exceptional and programmer-error failures head up the stack.
Warren
@Warren, Exceptions are the usual mechanism in .NET for returning errors to a caller. This is as true for event handlers as for any other code. Of course you could design your custom events so that an error code can be returned in the EventArgs, but it's an unusual design and can get complex it you want to allow multiple event handlers to return different codes. And the principle of least surprise says you should follow design patterns already established in the .NET Framework.
Joe
+2  A: 

Some of the answers here suggest it's bad to throw from an event handler ("creates havoc for your caller", "tends to lead to very difficult to handle situations, and unexpected behavior",...).

IMHO this is nonsense.

In the general case, it's perfectly OK to throw from an event handler. Other event handlers won't run of course - neither will the event handler that throws run to the end, nor any other code between the firing of the event and the point where it's caught. So what? It's perfectly normal that code is not executed when an exception is thrown - if you need to guarantee it's executed, then use a finally block.

Of course in any specific case you may want to consider which, if any, exceptions it is appropriate to handle, just as you would with any other code.

As always, there are no hard and fast rules that apply in all circumstances. One of the answers here says "event handlers should be fast ... and close to error free...". A counterexample is the ASP.NET Page.Load event.

A general rule in .NET is that it's almost always a bad idea to swallow all exceptions: this applies to event handlers just as it does to any other code.

So the answer to the original question "are there ever any circumstances in which it's acceptable for a method responsible for listening to an event to throw an exception" is very definitely yes.

Just as the answer to the question "are there ever any circumstances in which it's acceptable for a method responsible for listening to an event to swallow exceptions" is also yes.

Joe
I think calling it 'nonsense' is a bit unfair- certainly for events handled internally I'd probably consider throwing an exception if required, but if I'm writing a handler for a method on someone elses code, I wouldn't have thought it's appropriate to throw (or even allow) exceptions.
Flynn1179
What I'm calling 'nonsense' are the statements "creates havoc...", "very difficult to handle...", "unexpected behavior" which IMHO is just FUD. I'm not saying it's nonsense to catch or avoid throwing exceptions in an event handler some specific case. You should certainly consider what will happen when an exception is thrown from an event handler when designing your app.
Joe
If something unrecoverable has happened, then it makes sense for the event handler to throw an exception (or allow one to progress from a downstream call). It should just be understood that the class calling the handler through the event will (or at least should) allow the exception to propagate and ultimately kill the entire process or AppDomain, since it doesn't have sufficient context to know how to handle the exception. The other handlers shouldn't be called, since the AppDomain state may no longer be valid, so the question of whether it's breaking a contract is moot.
Dan Bryant