-->

dev csharp

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();
}

dev csharp

Conclusion and List of Posts

General consensus is that the new features are just small increments to improve productivity. They will help to clean up existing code. Less code is helpful to focus on the actual business logic instead of the clutter caused by the language.

For easy navigation I listed the links for all the previous posts:

Table of contents

  1. C# 6.0 New Features - Introduction
  2. Auto-Properties with Initializers
  3. Using statements for static classes
  4. Expression-bodied methods
  5. String interpolation
  6. Index initializers
  7. Null-conditional operators
  8. nameof operator
  9. Exception-handling improvements

Resources

dev csharp

Currently using statements are for namespaces only. With this new feature they can used for static classes as well. Like this:

using System.IO;
using System.IO.File;

namespace CSharp6Features
{
    class UsingStaticClass
    {
        public class StaticUsing
        {
            public StaticUsing()
            {
                File.WriteAllText("C:\test.txt", "test");
                WriteAllText("C:\test.txt", "test");
            }
        }
    }
}

I don’t think I liked this new feature. If you see a direct method call it feels like it’s a member of that method. But now it’s possible that method can be defined inside a static class somewhere else. I think it would just cause confusion and doesn’t add any benefit.