Exception Filter in C# 6.0

Overview

An Exception Filter is a new feature of C# 6.0 announced by Microsoft at the Visual Studio Connect() event on November 12, 2014, in New York, USA. On that day, many new features of C# 6.0, as well as some improvements in existing features, were announced, but in this article, we will learn about only one feature, Exception Filter. So before learning more about it, let's understand what it is and what its capabilities are.

Basically, Exception Filter is one of the new features of C# v6.0 that allows us to specify a conditional clause for each catch block. In other words, now we can write a catch block that will handle the exception of a specific type only when a certain condition is true that is written in an exception filter clause. First, we see a code snippet of an Exception Filter then we will learn more about it.

Example 1

using System;

class Program
{
    static void Main()
    {
        try
        {
            throw new Exception("ErrorType1");
        }
        catch (IndexOutOfRangeException ex) when (ex.Message == "ErrorType1")
        {
            Console.WriteLine("ErrorMessage: " + ex);
        }
        catch (IndexOutOfRangeException ex) when (ex.Message == "ErrorType2")
        {
            Console.WriteLine("ErrorMessage: " + ex);
        }
    }
}

Note. In the preceding code snippet, we saw that instead of handling all the exceptions of a specific type, we handled the specific exception. In this code, the try block is throwing the exception ErrorType1. We suppose that its base type is IndexOutOfRangeException. So it will be caught by both the catch block that is getting the IndexOutOfRangeException, but it will execute only by the first catch block that met the specified condition into the Exception Filter. Here ErrorType1 matches the first exception, in other words, if(ex.Message=="ErrorType1").

Now we know that the previous version of C# doesn't support Exception Filters. So let's type the same code snippet into an older version of C# and observe what type of syntax error it's showing for when we type in the same code snippet in C# 5.0.

ExceptionFilterNotSupported

Example 2

Program2

In the preceding program screen, the try block is throwing an exception, ErrorNo1. But in this program, we have two catch blocks in which the same exception type we are using is still executing only one catch block that meets the specified Exception Filter. In this program, ErrorNo1 matches the first exception filter, and that's why it executes only one statement that is written inside the match catch block.

Example 3

using System;
using System.Web;
using System.Web.UI;

public partial class YourPageClassName : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            // Calling the page which is not existent on this path
            Response.Redirect("~/Admin/Login.aspx");
        }
        catch (HttpException hex) when (hex.GetHttpCode() == 400)
        {
            lblMessage.Text = "This Page is Not Found!";
        }
        catch (HttpException hex) when (hex.GetHttpCode() == 500)
        {
            lblMessage.Text = "Internal Server Error Occurred!";
        }
    }
}

In the preceding code snippet, we are calling the page Login.aspx that does not exist at the given location, so a HttpException will throw 400. That is an exception number that tells that the given page is not found.

Summary

In this article, we learned about Exception Filterd, a new feature of C# 6.0.


Similar Articles