0

Is there a way to call an async method within a catch block?

At the moment I have an application which logs exceptions with an async / awaited method but I can't put this in a catch block because I'm told:

The 'await' operator cannot occur inside a catch or finally block of a try-statement, inside the block of a lock-statment or in an unsafe context.

At the moment I'm having to assign the exception to a variable inside the catch block and then call my await logException() afterwards.

Whilst this isn't the end of the world, it seems like there should / could be another / better way.

Any thoughts?

dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89

1 Answers1

2

Currently in C#5, no, there's no way to do it directly.

In C# 6 / Roselyn, it's already implemented.

rducom
  • 7,072
  • 1
  • 25
  • 39