locked
Exceptions in C++/Cx

    Question

  • Hi All,

     Some Exceptions are not properly handled in Metro C++/CX. for Eg:

    Suppose I Connect the stream socket using "ConnectAsync(hostName , serverPort, SocketProtectionLevel::Ssl))" and it raise first chance exceptions in some cases(due to firewall settings), so why these exceptions are handled?

    try
    {
    task<void> (socket->ConnectAsync(hostName , serverPort, SocketProtectionLevel::Ssl)).then([this](task<void> prevTask)
    {
    prevTask.get();
    });
    }
    catch(DisconnectedException^ ex)
    {
    return;
    }
    catch(FailureException^ ex)
    {
    return;
    }
    catch(InvalidArgumentException^ ex)
    {
    return;
    }
    catch(NotImplementedException^ ex)
    {
    return;
    }
    catch(NullReferenceException^ ex)
    {
    return;
    }
    catch(ObjectDisposedException^ ex)
    {
    return;
    }
    catch(OperationCanceledException^ ex)
    {
    return;
    }
    catch(OutOfMemoryException^ ex)
    {
    return;
    }
    catch(WrongThreadException^ ex)
    {
    return;
    }
    catch(Platform::Exception^ ex)
    {
    Platform::String^ str = ex->Message->ToString();
    int hRslt = ex->HResult;
    return;
    }


    Wednesday, October 24, 2012 8:23 AM

All replies

  • Can you please clarify your question? Is the problem that the exception is caught? That is uncaught? Or that Visual Studio breaks on the first chance exception?

    Without knowing the intent of the code I can't tell if it is wrong or not, but if you want to catch exceptions from the ConnectAsync call then you need to wrap the prevTask.get() call in the exception handler. The ones you have are for setting up the task. They won't be in scope when the task completion fires.

    See Handling errors in a task chain in the Asynchronous programming in C++ documentation for more details and an example code snippet.

    --Rob

    Thursday, October 25, 2012 12:07 AM
    Owner
  • Hi Rob,

    Thanks Rob, My Question is based on the connectAsyc() exception.

    for ex: When we try to connect some particular mail servers using stream socket, it won't reply and after some seconds the connectAsync() "Then" part will be executed and app will crash when get() called.

    I also checked the code like this, but result will be the same.

    try
    {
    task<void> (socket->ConnectAsync(hostName , serverPort, SocketProtectionLevel::Ssl)).then([this](task<void> prevTask)
      {
         try
           {
    	prevTask.get();
           }
         catch(Exception^ ex)
         {
    	return;
         }
       });
    }
    catch(DisconnectedException^ ex)
    {
    	return;
    }

    Anil.S

    Thursday, October 25, 2012 12:08 PM