Introduction
On the day of the Visual Studio Connect() event held on November 12, 2014, Microsoft introduced Visual Studio 2015 Preview with many new and exciting features for developers including the open source community and .NET 4.6 preview that are released for testing purposes. You can easily download the Visual Studio Preview 2015 from here.
So far, this is the first preview version of Visual Studio 2015 available to download and we hope it will become better in the future. So don't wait to try out this new release and help Microsoft to provide us the final release of the Visual Studio 2015 IDE as soon as possible and don't forget to submit your valuable feedback to Microsoft about your experience with Visual Studio 2015 Preview.
Don't forget to read my previous posts on this series: "A new feature of C# 6.0"
Getting to C#, C# 6.0 brought a set of very useful features with Visual Studio 2015 Preview and .NET 4.6. Let's have a look at one of the features, Exception Filter.
Exception Filtering
An Exception Filter is a feature already present in Visual Basic and F#. Microsoft now introduced this feature in C# 6.0 for developers to use in their code. Exception filters allow you to specify a conditional clause for each catch block. To use exception filters we need to declare them in the same line as where the catch block is declared, just like this: catch ( Exception e) if(filter condition). Instead of a catch block handling all exceptions of a specific type, they allow us to write catch blocks that handle when a certain condition is true. If the parenthesized expression after the "if" condition evaluates to true, the catch block will be executed, else the exception continues. Catching and rethrowing are preferred using exception filters because they leave the stack unharmed and if the exception later causes the stack to be dumped, we can see where it originated from.
Check out the following code snippet to understand how to write the Expression Filters in a catch statement of any try {} catch {} block.
try
{
int number = 0;
int x = 5 / number;
}
catch (DivideByZeroException) if (DateTime.Now.DayOfWeek != DayOfWeek.Tuesday)
{
Console.WriteLine("Sorry, today is not Tuesday");
}
catch (DivideByZeroException) if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
{
Console.WriteLine("Hurray It's Tuesday");
}
In the preceding code snippet, the try block is generating the exception DivideByZeroException and we have defined two DivideByZeroException catch blocks with different conditions. In the first condition we are checking if the present day is not Tuesday then the catch block will be executed and "Sorry, today is not Tuesday" will be printed. In the second DivideByZeroException catch block, we are checking if today's day is Tuesday, and if so then the second catch block will be executed, and "Hurray It's Tuesday" will be printed.
Demo application using Visual Studio 2013
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpFeatures
{
class ExceptionFilters
{
static void Main(String[] args) //using Visual Studio 2013
{
try
{
int number = 0;
int x = 5 / number;
}
catch (DivideByZeroException)
{
if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
{
Console.WriteLine("Yes it is Tuesday");
}
else
{
Console.WriteLine("No its not tuesday");
}
}
Console.WriteLine("Press any key to exit..");
Console.Read();
}
}
}
Demo application using Visual Studio 2015 Preview
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpFeatures
{
class ExceptionFilters
{
static void Main(String[] args) // using Visual Studio 2015
{
try
{
int number = 0;
int x = 5 / number;
}
catch (DivideByZeroException) if (DateTime.Now.DayOfWeek != DayOfWeek.Tuesday)
{
Console.WriteLine("Sorry, today is not Tuesday");
}
catch (DivideByZeroException) if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
{
Console.WriteLine("Hurray It's Tuesday");
}
Console.WriteLine("Press any key to exit");
Console.Read();
}
}
}
Summary
In this article, we learned how to use exception filtering in catch statements of any try {} catch {} block. That means that without catching the exception we are checking for a condition and if the condition is not met, the next catch block is considered.
So, what's your opinion about this feature? How will you use it in your project? Your comments are most welcome.