locked
How to catch exceptions caused by StreamSocket::ConnectAsync()?

    Question

  • There is one StreamSocket sample from this link:

    http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931

    If the host and port are both OK, the following code (from the example) works as expected.

        try
        {
        task<void>(socket->ConnectAsync(hostName, ServiceNameForConnect->Text, SocketProtectionLevel::PlainSocket))
        .then([this] (task<void> previousTask)
        {
            try
            {
                // Try getting all exceptions from the continuation chain above this point.
                previousTask.get();
                rootPage->NotifyUser("Connected", NotifyType::StatusMessage);

            }
            catch (Exception^ exception)
            {
                rootPage->NotifyUser("Connect failed with error: " + exception->Message, NotifyType::ErrorMessage);
            }
            catch(Platform::COMException^ ex)
            {
                rootPage->NotifyUser("COMException: " + ex->Message, NotifyType::ErrorMessage);
            }
            catch(...)
            {
                rootPage->NotifyUser("ex1", NotifyType::ErrorMessage);
            }
        });
        }
        catch (...)
        {
            rootPage->NotifyUser("ex2", NotifyType::ErrorMessage);
        }

    -------------------------------------

    But if we input an unavailable "hostName", then the code crash without catching with the following exception:

    First-chance exception at 0x74B14B32 in StreamSocket.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x02F0D88C. HRESULT:0x8007274D

    -------------------------------------

    If we input an unavailable port, then the code crash without catching with the following exception:

    First-chance exception at 0x74B14B32 in StreamSocket.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x02DFDA0C. HRESULT:0x8007274C

    -------------------------------------

    My question is how to catch all the exceptions of StreamSocket::ConnectAsync()?


    • Edited by willjing Wednesday, February 13, 2013 4:31 PM format
    Wednesday, February 13, 2013 4:22 PM

All replies

  • I also tried this piece of code:

         try
         {
            Windows::Foundation::IAsyncAction ^ op = socket->ConnectAsync(hostName, ServiceNameForConnect->Text, SocketProtectionLevel::PlainSocket);
            try
            {
                concurrency::task<void> connectTask(op);
                connectTask.wait();
            }
            catch (Exception^ exception)
            {
                rootPage->NotifyUser("Connect failed with error: " + exception->Message, NotifyType::ErrorMessage);
            }
            catch(...)
            {
                rootPage->NotifyUser("ex1", NotifyType::ErrorMessage);
            }
        }
        catch (...)
        {
            rootPage->NotifyUser("ex2", NotifyType::ErrorMessage);
        }

    -------------------------------------

    But if we input an unavailable "hostName", then the code crash without catching with the following exception:

    Unhandled exception at 0x5B98C531 (msvcr110d.dll) in StreamSocket.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.

    --------------------------------------

    The code doesn't work even for available host and port. What's wrong?

    Wednesday, February 13, 2013 4:27 PM
  • My environment:

    VS Premium 2012
    Version 11.0.51106.01 Update 1

    I also set the following capabilities for the project:

    Internet(Client & Server)

    Private Networks(Client & Server)

    • Edited by willjing Wednesday, February 13, 2013 4:35 PM one more
    Wednesday, February 13, 2013 4:30 PM
  • The problem here is that you have a try/catch block around setting up the task and you have a try/catch block in the results of the task, but you don't have a try/catch block which covers the actual running of the task. To do that you need to chain your task and wrap a try/catch block around getting the final results. This is described (with sample code) in Asynchronous programming in C++ (Windows Store apps) in the section "Handling errors in a task chain" and will look something like:

    try 
         {
         task<void>(socket->ConnectAsync(hostName, ServiceNameForConnect->Text, SocketProtectionLevel::PlainSocket))
         .then([this] (task<void> previousTask)
         {
            // ...
         }
        .then([](task<void> t) 
        {
    
            try
            {
                t.get();
                // .get() didn't throw, so we succeeded.
            }
            catch (Platform::COMException^ e)
            {
                // .get() threw, so we failed.
            }
    
        });
    
    


    --Rob

    Wednesday, February 13, 2013 4:53 PM
    Owner
  • We do have previousTask.get() in the 1st then() block. I think when the code steps into the 1st then() block, the ConnectAsync task has finished.

    Why the 2nd then() block can cover the actual running of the ConnectAsync task? I tried this but no effect.


    • Edited by willjing Wednesday, February 13, 2013 5:29 PM
    Wednesday, February 13, 2013 5:09 PM