Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled Exception error from a callback c#

I have a 3 tier architecture and send some data in the transport layer (TCP Client) using tcp sockets is this methods asynchronously using BeginSend method.

public void TransportData(Stream stream)
{
    try
    {
        SetTransporterState(StateObject.State.SendingData);
        if (clientSock.Connected)
        {
            ASCIIEncoding asen = new ASCIIEncoding();
            stream.Position = 0;
            byte[] ba = GetStreamAsByteArray(stream);
           if (clientSock.Connected)
              clientSock.BeginSend(ba, 0, ba.Length, SocketFlags.None, new AyncCallback(SendData), clientSock);
           else
               throw new SockCommunicationException("Socket communication failed");

         }
         catch (SocketException sex)
         {
             throw sex;
         }
         catch (SockCommunicationException comex)
         {
             bool rethrow = ExceptionPolicy.HandleException(comex, "TransportLayerPolicy");
         if (rethrow)
         {
             throw;
         }
       }
     }
   }
   catch (SocketException soex)
   {
     throw soex;
   }
   catch (SockCommunicationException comex)
   {
      bool rethrow = ExceptionPolicy.HandleException(comex, "TransportLayerPolicy");
      if (rethrow)
      {
         throw;
      }                
    }
    catch (Exception ex)
    {
       LoggingMessageHelper.LogDeveloperMessage(ex.Message + Environment.NewLine + ex.StackTrace, 1, TraceEventType.Critical);
        bool rethrow = ExceptionPolicy.HandleException(ex, "TransportLayerPolicy");
        if (rethrow)
        {
          throw;
        }
     }
  }

The callback code SendData() is below

    private void SendData(IAsyncResult iar)
    {
        Socket remote = null;
        try
        {
            remote = (Socket)iar.AsyncState;
            try
            {
                int sent = remote.EndSend(iar);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (remote.Connected )
            {
                remote.BeginReceive(data, 0, size, SocketFlags.None,
                              new AsyncCallback(ReceiveData), remote); 
            }
            else
                throw new SockCommunicationException("Communication Failed");
        }
        catch (SocketException soex)
        {
            throw new SockCommunicationException("Communication Failed");
        }
        catch (SockCommunicationException comex)
        {
            bool rethrow = ExceptionPolicy.HandleException(comex, "TransportLayerPolicy");
            if (rethrow)
            {
                throw;
            }
        }

When the server disconnects, the client does not know until it sends some data, therefore the Connected property is true. Then the line remote.BeginReceive() throws the SocketException which I try to capture and throw a custom exception (sockCommunicationException).

However when I do this I get an unhandled exception. I would like to bubble this error to the UI through the Business layer. When an exception like this is raised in the callback method, where does it get raised to?

How can I avoid this undhandled exception and bubble this exception to the UI layer.

Please help.

Thanks in advance!

like image 733
user466898 Avatar asked Dec 12 '10 18:12

user466898


People also ask

What is an unhandled exception in C?

An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.

What does unhandled exception error mean?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions. Learn about the definition and examples of unhandled exceptions, and explore programming and exception handlers. Updated: 01/04/2022.

What happens to an unhandled exception?

If an unhandled exception condition happens in a secondary thread and moves all the way to the first invocation in the thread without being handled, the resulting action will be to terminate the thread. During this percolation, if the exception hits a control boundary and is not handled, it may terminate the process.


2 Answers

Yes, doesn't work. The callback is executed on a threadpool thread, there's nobody there to catch the exception. You'll need to, say, raise an event that the UI can subscribe to. Marshaling is required, the UI code needs to use something like Control.BeginInvoke() if it needs to update the UI.

like image 66
Hans Passant Avatar answered Sep 17 '22 05:09

Hans Passant


This is a common requirement.

Solution is usually by SendData having access to some local method to notify the client of the result of the operation and possibly showing in the UI.

Now you need to achieve this in the middle-tier which is different. Your client calls the server which in turn calls async method and triggers the operation. If the client needs to know about the result and its exceptions, it has to poll the server and check for the result - and you would have to store the result (on the server or some database) until client retrieves it or throw away after a time-out - cleanup itself becomes complex.

Alternatively it could use a Duplex method for the Server to communicate back to the Client. This solution sounds great but to be fair is awful for various reasons which is outside the scope of this question.

like image 40
Aliostad Avatar answered Sep 18 '22 05:09

Aliostad