Suppose I have a method which changes the state of an object, and fires an event to notify listeners of this state change:
public class Example
{
public int Counter { get; private set; }
public void IncreaseCounter()
{
this.Counter = this.Counter + 1;
OnCounterChanged(EventArgs.Empty);
}
protected virtual void OnCounterChanged(EventArgs args)
{
if (CounterChanged != null)
CounterChanged(this,args);
}
public event EventHandler CounterChanged;
}
The event handlers may throw an exception even if IncreaseCounter
successfully completed the state change. So we do not have strong exception safety here:
The strong guarantee: that the operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started.
Is it possible to have strong exception safety when you need to raise events?