Await methods are not supported in catch and finally block for .Net 4.0, solution ExceptionDispatchInfo

If you are using .net 4.5 and use async/await you will realize that you cannot await in the catch block, the new C# 6 language overcomes this limitation.

 

in .Net 4.5 you can us the ExceptionDispatchInfo to capture the exception and throw as required, example below

———————————————————————————————————————————————————

ExceptionDispatchInfo capturedException = null;

try
{
SomeMethodCall()
}
catch (Exception ex)
{
capturedException = ExceptionDispatchInfo.Capture(ex);
}
if (capturedException != null)
{

await LogExceptionMethod(), //log exceptions to an external endpoint

}

throw exception;

Advertisement