Javatpoint Logo
Javatpoint Logo

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

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

C# Exception Filter Example

Output

A new task is executing...

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

C# Exception Filter Example2

Output

Array Exception Occured
A new task is executing...

Next TopicC# Await





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA