Here are four approaches.
In approach "A", the Exception is multi-cast to all subscribers. This is done by including the Exception instance as an "innerException" field in your custom EventArgs class.
In approach "B", the Exception is handled "out-of-band" (not multi-cast, not part of the event mechanism) by calling a separate delegate.
In approach "C", you have an application-level exception handler.
You want to inform it that this exception happened as part of processing ReceiveCompleted.
Do this by defining (and throwing) a ReceiveCompletedException, which has an "innerException" field to contain the actual exception.
In approach "D" (no code given below) you don't care that the exception happened in ReceiveCompleted code. You just need a generic place to handle exceptions. This is known as "application-level exception handling". See Catch-all error handling on application level?
Approach A:
// ========== 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
}
}
}
Approach 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.
}
}
Approach 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
}
// ...
catch { throw; }
bits do not contribute to anything, and do not do anything, and should just be removed. – TranslocateInvoke
method is visible, and so the answer to it is correct. You need to put thecatch
code around yourInvoke
. – TranslocateEndReceive
, and that there isEndInvoke
inside it too. In asynchronous calls you catch the exceptions inEndInvoke
, notBeginInvoke
. – Translocate