这里有四种方法。
在方法“A”中,异常对所有订阅者进行多播。这是通过将 Exception 实例作为“innerException”字段包含在您的自定义 EventArgs 类中来完成的。
在方法“B”中,通过调用单独的委托来“带外”处理异常(不是多播,不是事件机制的一部分)。
在方法“C”中,您有一个应用程序级异常处理程序。您想通知它此异常是作为处理 ReceiveCompleted 的一部分发生的。通过定义(并抛出)一个 ReceiveCompletedException 来做到这一点,它有一个“innerException”字段来包含实际的异常。
在方法“D”(下面没有给出代码)中,您不关心异常发生在 ReceiveCompleted 代码中。您只需要一个通用的地方来处理异常。这称为“应用程序级异常处理”。请参阅 应用程序级别的全面错误处理?
方法一:
// ========== A: multi-cast "innerException" integrated into EventArgs ==========
public class ReceiveCompletedEventArgs_A
{
public string myData; // Set on successful events (no exception).
public Exception innerException; // Set when there is an exception.
}
public delegate void ReceiveCompletedEventHandler_A(object sender, ReceiveCompletedEventArgs_A args);
// The Publisher of ReceiveCompleted event, with "innerException" mechanism.
public class RCPublisher_A
{
public event ReceiveCompletedEventHandler_A ReceiveCompleted;
public void OnRaiseReceiveCompletedEvent(string myData)
{
ReceiveCompletedEventArgs_A rc;
try
{
rc = new ReceiveCompletedEventArgs_A { myData = myData };
// Uncomment below line, to see an exception being handled.
//throw new Exception("Testing exception handling");
}
catch (Exception ex)
{
rc = new ReceiveCompletedEventArgs_A { innerException = ex };
}
if (ReceiveCompleted != null)
ReceiveCompleted(this, rc);
}
}
// A Subscriber of ReceiveCompleted event, with "innerException" mechanism.
public class RCSubscriber_A
{
public void Initialize(RCPublisher_A rcp)
{
rcp.ReceiveCompleted += OnReceiveCompleted;
}
private void OnReceiveCompleted(object sender, ReceiveCompletedEventArgs_A rc)
{
if (rc.innerException != null)
{
// Handle the exception
}
else
{
// Use rc.myData
}
}
}
方法 B:
// ========== B: "Out-of-band" handling of exceptions; not multi-cast ==========
// (Successful events are multi-cast, but exceptions are sent to a single delegate.)
public class ReceiveCompletedEventArgs_B
{
public string myData; // Set on successful events (no exception).
}
public delegate void ReceiveCompletedEventHandler_B(object sender, ReceiveCompletedEventArgs_B args);
public delegate void ExceptionDelegate(Exception ex);
// The Publisher of ReceiveCompleted event, with out-of-band Exception handling.
public class RCPublisher_B
{
// Called when the event is successful (no exception).
public event ReceiveCompletedEventHandler_B ReceiveCompleted;
// Called when there is an exception.
public ExceptionDelegate exceptionDeleg;
public void OnRaiseReceiveCompletedEvent(string myData)
{
try
{
ReceiveCompletedEventArgs_B rc = new ReceiveCompletedEventArgs_B { myData = myData };
// Uncomment below line, to see an exception being handled.
//throw new Exception("Testing exception handling");
if (ReceiveCompleted != null)
ReceiveCompleted(this, rc);
}
catch (Exception ex)
{
if (exceptionDeleg != null)
exceptionDeleg(ex);
// What to do if there is no exceptionDeleg:
// If below line is commented out, unhandled exceptions are swallowed.
// Uncomment, to throw them to your app-level exception handler.
else throw;
}
}
}
// A Subscriber of ReceiveCompleted event, with out-of-band Exception handling.
public class RCSubscriber_B
{
public void Initialize(RCPublisher_B rcp)
{
rcp.ReceiveCompleted += OnReceiveCompleted;
// CAUTION: Overrides any other exception delegate.
// If you need multi-casting of the exception, see Approach A.
rcp.exceptionDeleg = RCExceptionOccurred;
}
private void OnReceiveCompleted(object sender, ReceiveCompletedEventArgs_B rc)
{
// Use rc.myData
}
private void RCExceptionOccurred(Exception ex)
{
// Use ex.
}
}
方法 C:
// ========== C: Wrap "innerException" into custom Exception, for app-level handler ==========
// Similar to B, but instead of adding ExceptionDelegate and exceptionDeleg,
// Define a custom exception type, and throw it.
// Catch it inside your app-level handler.
public class ReceiveCompletedException : Exception
{
public Exception innerException;
}
public class RCPublisher_C
{
public event ReceiveCompletedEventHandler_B ReceiveCompleted;
public void OnRaiseReceiveCompletedEvent(string myData)
{
ReceiveCompletedEventArgs_B rc;
try
{
rc = new ReceiveCompletedEventArgs_B { myData = myData };
// Uncomment below line, to see an exception being handled.
//throw new Exception("Testing exception handling");
if (ReceiveCompleted != null)
ReceiveCompleted(this, rc);
}
catch (Exception ex)
{
throw new ReceiveCompletedException{ innerException = ex };
}
}
}
// ...
// In your app-level handler:
// ...
catch (ReceiveCompletedException rce)
{
// If it gets here, then an exception happened in ReceiveCompleted code.
// Perhaps you have some graceful way of restarting just that subsystem.
// Or perhaps you want a more accurate log, that instead of just saying
// "Exception XYZ happened" (the inner exception), logs that it was
// ReceiveCompleted that has the problem.
// use rce.innerException
}
// ...