I'm trying to handle task exceptions without await. I am trying to use ContinueWith with TaskContinuationOptions.NotOnRanToCompletion. But i have one main problem - in test program everything goes well and works only code from ContinueWith. But in my main program Exception still goes to catch block and to ContinueWith (function doesn't stop working but I want to work with this exceptions in other way).
Small example looks like this
class Program
{
static void Main(string[] args)
{
try
{
Testim();
}
catch (Exception)
{
Console.WriteLine("catch");
}
}
static void method()
{
HttpClient cl=new HttpClient();
cl.PostAsync("http://oru.uri", null);
}
static void Testim()
{
HttpClient cl = new HttpClient();
Task t = cl.PostAsync("http://oru.uri", null);
t.ContinueWith((Task t1) => { Console.WriteLine("success"); }, CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current);
t.ContinueWith((Task t1) => { Console.WriteLine("error"); }, CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Current);
Console.ReadKey();
}
}
In test project everything okay, but in my other project exeptions handle 2 times - in catch and in continuewith and catch executes first.
Problem of my code (not this example) was that I Wait() task. But when I receive exception before Wait() then Wait generate Exception