c# - What is the proper way to propagate exceptions in continuation chains? -


what proper way propagate exceptions in continuation chains?

t.continuewith(t2 =>  {      if(t2.exception != null)          throw t2.exception;       /* other async code. */ }) .continuewith(/*...*/);     t.continuewith(t2 =>  {      if(t2.isfaulted)          throw t2.exception;       /* other async code. */ }) .continuewith(/*...*/);  t.continuewith(t2 =>  {      if(t2.exception != null)          return t2;       /* other async code. */ }) .continuewith(/*...*/);     t.continuewith(t2 =>  {      if(t2.isfaulted)          return t2;       /* other async code. */ }) .continuewith(/*...*/);   t.continuewith(t2 =>  {      t2.wait();       /* other async code. */ }) .continuewith(/*...*/);  t.continuewith(t2 =>  {           /* other async code. */ }, taskcontinuationoptions.notonfaulted) // don't think 1 works expected .continuewith(/*...*/); 

the taskcontinuationoptions.onlyon... can problematic because cause continuation cancelled if condition not met. had subtle problems code wrote before understood this.

chained continuations quite hard right. by far easiest fix use new .net 4.5 await functionality. allows ignore fact you're writing asynchronous code. can use try/catch blocks might in synchronous equivalent. .net 4, available using async targeting pack.

if you're on .net 4.0, straightforward approach access task.result antecendent task in each continuation or, if doesn't return result, use task.wait() in sample code. however, you're end nested tree of aggregateexception objects, you'll need unravel later on in order 'real' exception. (again, .net 4.5 makes easier. while task.result throws aggregateexception, task.getawaiter().getresult()—which otherwise equivalent—throws underlying exception.)

to reiterate not trivial problem, might interested in eric lippert's articles on exception handling in c# 5 async code here , here.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -