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;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: