title: Exception handling in delegate
body: <p>I have the following code which gives me an "<strong>Exception was unhandled by user code</strong>" when it tries to throw an error:</p>
  
  <pre><code>private static void _msgQ_RecieveCompleted(object sender, ReceiveCompletedEventArgs e)
  {
      try
      {
          //queue that have received a message
          MessageQueue _mq = (MessageQueue)sender;
  
          //get the message off the queue
          Message _mqmsg = _mq.EndReceive(e.AsyncResult);
          throw new Exception("This is a test exception by Tim");
  
          //set the values back into a formatted struct 
          //now process your SQL....
          Azure_SQL _azuresql = new Azure_SQL();
          _azuresql.writeMessageToStorage((_TwitterStreamFeed)_mqmsg.Body);
  
          //refresh queue just in case any changes occurred (optional)
          _mq.Refresh();
  
          //tell MessageQueue to receive next message when it arrives
          _mq.BeginReceive();
  
          return;
      }
      catch 
      {
          throw;
      }
  }
  </code></pre>
  
  <p>It is called by the following method (previously the snippet):</p>
  
  <pre><code>public void MSMQ_GetMessage(string _MQ_Path)
          {
              try
              {
  
                  //set the correct message queue
                  MessageQueue _msgQ = new MessageQueue(_MQ_Path, QueueAccessMode.ReceiveAndAdmin);
                  //set the format of the message queue
                  _msgQ.Formatter = new XmlMessageFormatter(new Type[] { typeof(_TwitterStreamFeed) });
                  try
                  {
                      _msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(_msgQ_RecieveCompleted);
                  }
                  catch
                  {
                      throw;
                  }
  
                  IAsyncResult _result = _msgQ.BeginReceive();
                  _asyncList.Add(_result); // asyncList is a global variable of type System.Collections - &gt; this allows the callback to remain open and therefore nit garbage collected while the async thread runs off on it's own
              }
              catch (Exception _ex)
              {
                  throw new Exception("_msgQ_get Message threw the following error :- " + _ex);
              }
              catch
              {
                  throw;
              }
  
          }
  </code></pre>
  
  <p>Can you help me understand why the error isn't thrown back to the <code>ReceiveCompletedEventHandler</code> call? I get that it's executing the code on a different thread, but I don't understand from the <strong>MSDN</strong> examples how to capture the exception. I was expecting the Exception to be return to the call <strong>try/catch</strong> block.</p>