You need to ensure that the second operation is invoked only if the data processing operation throws an unhandled exception

You use the Task.Run() method to launch a long-running data processing operation. The data
processing operation often fails in times of heavy network congestion.
If the data processing operation fails, a second operation must clean up any results of the first operation.
You need to ensure that the second operation is invoked only if the data processing operation throws an
unhandled exception.
What should you do?

You use the Task.Run() method to launch a long-running data processing operation. The data
processing operation often fails in times of heavy network congestion.
If the data processing operation fails, a second operation must clean up any results of the first operation.
You need to ensure that the second operation is invoked only if the data processing operation throws an
unhandled exception.
What should you do?

A.
Create a TaskCompletionSource<T> object and call the TrySetException() method of the object.

B.
Create a task by calling the Task.ContinueWith() method

C.
Examine the Task.Status property immediately after the call to the Task.Run() method.

D.
Create a task inside the existing Task.Run() method by using the AttachedToParent option.



Leave a Reply 8

Your email address will not be published. Required fields are marked *


MarcoJacob

MarcoJacob

So I would say A, or?

vamsee mudradi

vamsee mudradi

Looks like its A wherein, we create an object of tcs and then if exception, the Excption ex will catch it and within it, we TrySetException wherein we create another Custom exception====
catch(Exception ex)
{TrySetException(new CustomException())}.
But catch here is that it would only Try to Set an exception (need not always be 100% sucessfull to Set the Exception). So, would go with B which is popular answer to this question.

Stan

Stan

B.You have to Create a task by calling the “Task.ContinueWith()” method

Jack

Jack

B
Task.Status should have a value of faulted if exception unhandled

Daniel

Daniel

Answer id B

static void Main(string[] args)
{
var task = Task.Run(() =>
{
throw new Exception();
}).ContinueWith(ContinuationAction);

task.Wait();

Console.ReadKey();
}

private static void ContinuationAction(Task task)
{
if (task.IsFaulted)
{
return;
}

Console.WriteLine(“Done.”);
}

Artem

Artem

TaskContinuationOptions can be used:
Task.Run(() => { }).ContinueWith((task) => { }, TaskContinuationOptions.OnlyOnFaulted);

John

John

This is the correct way of doing it

Mario

Mario

I dont get it… What about the “second operation is invoked ONLY IF the data processing operation throws an unhandled exception”, While Task.ContinueWith() continues even if it does not throws exceptions.