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 was 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 it's 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: I

 

  1. try  
  2. {  
  3.    throw new Exception("ErrorType1");  
  4. }  
  5. catch(IndexOutOfRangeException ex) if(ex.Message=="ErrorType1")  
  6. {  
  7.    WriteLine("Error Message : "+ ex);  
  8. }  
  9. catch(IndexOutOfRangeException ex) if(ex.Message=="ErrorType2")  
  10. {  
  11.    WriteLine("Error Message : "+ ex);  
  12. }  

 

Notes

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 it's 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: II

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 met the specify Exception Filter. In this program ErrorNo1 matches the first exeption filter and that's why it's executing only one statement that is written inside the match catch block.

Example: III

 

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     try  
  4.     {  
  5.       //Calling the page which is not exixt on this path  
  6.         Response.Redirect("~/Admin/Login.aspx");  
  7.     }  
  8.     catch (HttpException hex) if(hex.GetHashCode==400)  
  9.     {   
  10.         lblMessage.Text="This Page is Not Found !";  
  11.     }  
  12.     catch (HttpException hex) if(hex.GetHashCode==500)  
  13.     {   
  14.         lblMessage.Text="Internal Server Error Occured !";  
  15.     }  
  16. }  

 

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.