0

This is an example from https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/exception-handling-task-parallel-library I have tried to execute it in Visual Studio 2022, C#10, Net 6 but exception is not propagated to try catch block. We can emulate some delay to force it to propagate (via sleep, or task.Wait(10), ...) but this is not documented. Is this some kind of compiler optimization or is it bug?

public class Program
{
    public static void Main()
    {
        var task = Task.Run(
           () => {
               //Thread.Sleep(10); //if uncomment then exception is propagated  
               throw new CustomException("This exception is expected!");
           });

        try
        {
            task.Wait(); //we can use task.Wait(1) then exception is caught  
        }
        catch (AggregateException ae)
        {
            foreach (var ex in ae.InnerExceptions)
            {
                // Handle the custom exception.
                if (ex is CustomException)
                {
                    Console.WriteLine(ex.Message);
                }
                // Rethrow any other exception.
                else
                {
                    throw ex;
                }
            }
        }
    }
    class CustomException : Exception
    {
        public CustomException(string s) : base(s) { }
    }
}

enter image description here

Oleg
  • 624
  • 6
  • 12
  • 2
    I'm unable to reproduce your problem in [LINQPad](http://share.linqpad.net/4fkxkg.linq) or [dotnetfiddle](https://dotnetfiddle.net/7cKUg9). In all cases I see "This exception is expected!" – StriplingWarrior Sep 29 '22 at 19:15
  • The same at [sharplab](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0AXEUCuA7NAExAGoAfAAQCYBGCgBgAIKaBWAbgeZoDoAVABawAhoQCWeAObsAsACh5AN2FRGGYQGcA1owC8zJDwBK+ABTzGlxqYCUegHyMA3hatWA9O8aCRhHgGUAGxgYAAdTGnobdkZPMQAzRnxIAFsUmDwMNQEMxhgEMDCMMQg8RjENRlCoCFDhSWEMGEJLVzdsmoB3RjwYboBhHA0MCBSAUQKikrxTACJBCrzJ0OLS8sr80JgwJsIAQlnotsYAXyOFOSsMKABPNpdL9vVtHgB1YTEMWxjGNpO2sCNMACawAQUkklgDSaE0KK2mjGEMBs92OjHi0BgwmB1mUqny5TKSJ4AEk8L0oLCpqUNCjHu12g8GcyrAlrATFoNhqMqfDSnSWSymYKRSwAJymfI8ACyMA0GnqyNk9JFVn+KtVeUCGhgaNVws1DIwQgg3XyysNar1DPVLNtVmO1EYXJG42WqzKIEYvI9xwN7QoAGZnUNXT7pqYWExaYwvcBNDBTDGnKdjicgA==). – Guru Stron Sep 29 '22 at 19:39
  • @GuruStron try it in visual studio 2022 (on Mac OS, or Windows) – Oleg Sep 29 '22 at 20:23
  • @Oleg works as expected. – Guru Stron Sep 29 '22 at 20:34
  • @Oleg I've tried in Visual Studio 2022 with .NET 6. I'm not able to reproduce your problem. – MarsRoverII Sep 29 '22 at 20:54
  • @MarsRoverII I have Visual Studio Community 2022 for Mac Version 17.3.3 (build 10) Installation UUID: c8e1e8c1-f37c-48dd-a661-c39bd2576b3c Runtime .NET 6.0.5 (64-bit) Architecture: X64 – Oleg Sep 29 '22 at 21:07
  • @Oleg For me it's Visual Studio Enterprise 2022 on Windows OS. Not sure about the Visual Studio Community 2022 on Mac. – MarsRoverII Sep 29 '22 at 21:16
  • Check [this solution](https://stackoverflow.com/a/32067091/593045) and posts around it. Be aware that multithreading problems depend upon available physical/hyperthreading cores and workload whether it is reproducible or not. – BitLauncher Sep 29 '22 at 21:53
  • I would suggest opening the issue on dotnet github page and adding all repro info (OS, VS, .NET versions, hardware specs) – Guru Stron Sep 30 '22 at 22:21

0 Answers0