cant catch the Exception in the Concurrency Runtime

金旺 王 25 Reputation points
2023-08-02T07:14:24.94+00:00

Exception thrown at 0x7693483F (KernelBase.dll) in TongJiKeJiVR.exe: WinRT originate error - 0x80072746 : 'An existing connection was forcibly closed by the remote host.'.

Exception thrown at 0x7693483F in TongJiKeJiVR.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x1547FB40. HRESULT:0x80072746 An existing connection was forcibly closed by the remote host.

WinRT : An existing connection was forcibly closed by the remote host.

Concurrency::create_task(_writer->StoreAsync()).then(
            [&](Concurrency::task<unsigned int> writeTask)
        {
            try
            {
                // Try getting an exception.
				        writeTask.get();
            }
            catch (...)
            {
                _socket = nullptr;
            }
			});
		
another way i tried
		
		 Concurrency::create_task(_writer->StoreAsync()).then(
            [&](Concurrency::task<unsigned int> writeTask)
        {
            try
            {
                // Try getting an exception.
				        writeTask.get();
            }
            catch (...)
            {
                _socket = nullptr;
            }
			      return writeTask;
			})
			.then([](concurrency::task< unsigned int > t)
		 {
			  try 
			  {
				   t.get();
				   // .get() didn't throw, so we succeeded.
			  }
			  catch (...)
			  {
				     dbg::trace(L" StoreAsync call failed with error2:");
			  }
		});
		
i have found similar question in https://www.it1352.com/486319.html & https://learn.microsoft.com/en-us/cpp/parallel/concrt/exception-handling-in-the-concurrency-runtime?view=msvc-170&redirectedfrom=MSDN
but cant fix my issue with the way in the page
Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,451 questions
HoloLens Development
HoloLens Development
HoloLens: A family of Microsoft self-contained, holographic devices that enable engagement with digital content and interaction with holograms in the surrounding environment.Development: The process of researching, productizing, and refining new or existing technologies.
365 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 13,516 Reputation points Microsoft Vendor
    2023-08-16T12:34:15.8366667+00:00

    Hi @金旺 王 ,

    Welcome to Microsoft Q&A!

    I use your code and test it in UWP C++/WinRT project, and the wait() cannot be used in UWP.

    If you want to get Exception in concurrency::task<void> t, you need to re-throw the Exception in Concurrency::task<void> previousTask to pass the exception to task t.

    Concurrency::create_task(_listener->BindServiceNameAsync("555")).then(
    		[this](Concurrency::task<void> previousTask)
    		{
    			try
    			{
    				// Try getting an exception.
    				// previousTask.get();
    				throw ref new Platform::Exception(E_FAIL);
    			}
    			catch (...)
    			{
    				OutputDebugString(L"Platform::Exception");
    				throw;
    			}
    			return previousTask;
    		}).then([](concurrency::task< void > t)
    			{
    				try
    				{
    					t.get();
    					// .get() didn't throw, so we succeeded.
    				}
    				catch (...)
    				{
    					OutputDebugString(L"Platform::Exception 2");
    				}
    			}).wait();
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. 金旺 王 25 Reputation points
    2023-08-23T09:06:04.03+00:00

    hi Junjie Zhu:

    Thanks for your support, I guess i have found the answer, someone has raised the same issue 10 years ago, please refer to below link.

    https://social.msdn.microsoft.com/forums/en-US/b55dbfad-6a00-4e42-b481-080fbcfa3ef1/ccx-streamsocket-wrong-ip-or-port-or-connection-error?forum=winappswithnativecode

    but i my VS2017 , "If there is a handler for the exception, the program maybe run safety" was deleted