c# - TaskContinuationOptions.OnlyOnFaulted vs try catch -


what's difference between

var task3 = task.run(() => performlongtask()).continuewith(t => log.error(t.exception), taskcontinuationoptions.onlyonfaulted); 

and

var task3 = task.run(() => performlongtask());  private void performlongtask() {   try   {   //......   }   catch (exception ex)   {      log.error(ex);   } } 

i tried running both examples. on surface, appear behave same.

is 1 approach better other?

in case, they're same. however, if don't own innards of "performlongtask", can't wrap contents in try-catch either. also, task's status set rantocompletion way, if threw exception. if have continuation on task, have deal antecedent task potentially not having valid result.

i suppose short of in former case, exception handling logic contained in separate task, decoupled rest.


Comments