TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Exception Filters

C# Exception Filters for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Exception Filters

C# Exception Filters is a feature of C# programming language. It is introduced in version C# 6.0. It allows us to specify condition along with a catch block.

C# provides when keyword to apply a condition (or filter) along with catch block.

A catch block will execute only when the condition is true. If the condition is false, catch block is skipped and compiler search for next catch handler.

C# Exception Filters is used for logging purpose.

C# Exception Filter Syntax

catch (ArgumentException e) when (e.ParamName == "?"){  }  

In the following example, we are implementing exception filter. It executes only, if compiler throws an IndexOutOfRangeException exception.

C# Exception Filter Example

using System;
namespace CSharpFeatures
{
    class ExceptionFilter
    {
        public static void Main(string[] args)
        {
            try
            {
                int[] a = new int[5];
                a[10] = 12;
            }catch(Exception e) when(e.GetType().ToString() == "System.IndexOutOfRangeException")
            {
                // Executing some other task
                SomeOtherTask();
            }
        }
        static void SomeOtherTask()
        {
            Console.WriteLine("A new task is executing...");
        }
    }
}

Output

A new task is executing...

In the following example, we are throwing exception explicitly that matches with when condition.

C# Exception Filter Example2

using System;
namespace CSharpFeatures
{
    class ExceptionFilter
    {
        public static void Main(string[] args)
        {
            try
            {
                // Throwing Exception explicitly
                throw new IndexOutOfRangeException("Array Exception Occured");
            }catch(Exception e) when(e.Message == "Array Exception Occured")
            {
                Console.WriteLine(e.Message);
                SomeOtherTask();
            }
        }
        static void SomeOtherTask()
        {
            Console.WriteLine("A new task is executing...");
        }
    }
}

Output

Array Exception Occured
A new task is executing...

Next TopicC# Await




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf