Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does try/catch work outside of Task.Factory.StartNew? [duplicate]

Tags:

.net

c#-4.0

Will the following catch an exception that occurs inside of StartNew()? It doesn't seem to.

   try
   {
      Task.Factory.StartNew(() =>
      {
       //do something
      });
    }
    catch(Exception ex)
    {
      //log it
    }
like image 281
4thSpace Avatar asked Oct 02 '22 05:10

4thSpace


1 Answers

No. Your try block will exit after the new task is created.

You can catch exceptions though. Please see: Catching Error when using Task.Factory for more information.

like image 80
Francis MacDonald Avatar answered Oct 13 '22 10:10

Francis MacDonald