C# 6.0 New Features - Exception-Handling Improvements

development, csharp comments edit

There are 2 improvements on exception handling:

  1. Exception filters
  2. Using await in catch and finally blocks

Exception Filters

Visual Basic and F# already have this feature and now C# has it too! the way it works is basically defining a condition for the catch block (example taken from Channel 9 video):

try
{

}
catch(ConfigurationException e) if (e.IsSevere)
{

}

I think it can make exception handling more modular. Also it’s better than catching and rethrowing in terms of we don’t lose information about the original exception.

Using await in catch and finally blocks

Like most people I hadn’t noticed we couldn’t do that already! Apparently it was just a flaw in the current implementation and they closed that gap with this version

try
{

}
catch(ConfigurationException e) if (e.IsSevere)
{
	await LogAsync(e);
}
finally
{
	await CloseAsync();
}

Comments