0

I am looking into the exception handling on tasks with below program. The program runs fine and exception was handled if I run with Ctrl + F5. However, it stopped at the throw exception line when I run with F5.

static void Main(string[] args)
    {
        try
        {
            Task.Run(() => test()).Wait();
        }
        catch (Exception)
        {
            Console.WriteLine("Caught");
        }
    }

    private static Task test()
    {
        throw new NotImplementedException();
    }

I wonder if my test program is working(caught the exception). Could this relate to my Visual Studio setting(I am using VS2013). Thank you

Sean
  • 981
  • 1
  • 9
  • 19

1 Answers1

0

You are explicitly throwing an exception and not catching it. That causes the problem when you build the solution. Catch the Exception which is thrown using try/catch

 private static Task test()
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (NotImplementedException)
            {
                Console.WriteLine("NotImplementedException");                
            }

        }

you can write a general exception also instead of NotImplementedException.

Akansha
  • 933
  • 7
  • 18