0
     [HttpPost]
 public async Task<JsonResult>    
    {
     await sendNotification(responce);
    }

I want to catch exception occured in sendnotification method. How can I call the same using Task.Factory ??

1
  • The caller needs to handle the exceptions - they are wrapped by the Task and sent back in the result (check Task.Result). Obviously you can try/catch inside the method and throw the appropriate exception Commented Apr 15, 2014 at 8:51

3 Answers 3

2

You just write a try/catch block, as such:

[HttpPost]
public async Task<JsonResult>    
{
  try
  {
    await sendNotification(responce);
  }
  catch (Exception ex)
  {
    ...
  }
}

You don't need Task.Factory for this.

Sign up to request clarification or add additional context in comments.

2 Comments

I do this but nothing happens, the exception that is thrown is a Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) and any evidence of the real exception is lost as inner exception is null.
@simbolo: Please post a repro and the exception details as a separate question.
0

Exception Handling of sendNotification() task depends on how the task is coded. In one way, above Stephen Cleary's answer is right but not sure it fixes your issue.

You can go through below easy understandable articles to understand Exception Handling in Async programming...

Basic exception handling in Asynchronious programming in C#.

Exception object behaves differently while handling exceptions in parallel tasks in Asynchronous programming.

1 Comment

Welcome to Stack Overflow! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
0

Also look into below link..

Handling the AggregateException using Handle method in Asynchronous programming.

1 Comment

Please avoid link only answers, they have no value if link get broken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.