Tuesday 7 November 2017

Exception Filter in C# 6.0

In this article, We will use How to use Exception Filter in C# 6.0.

Exception filter is a new Concept in C# 6.0, Where we can add if Condition in Catch Statement and if that Condition is true then that particular catch Statement will execute. But if the Condition fails then the final catch Statement will execute.

1.   using System;  
2.   using System.Collections.Generic;  
3.   using System.Linq;  
4.   using System.Text;  
5.   using System.Threading.Tasks;  
6.   using System.Console;  
7.   namespace project5  
8.   {  
9.       class Program  
10.     {  
11.         static void Main(string[] args)  
12.         {  
13.             int val1 = 0;  
14.             int val2 = 0;  
15.             try  
16.             {  
17.                 WriteLine("Enter first value :");  
18.                 val1 = int.Parse(ReadLine());  
19.                 WriteLine("Enter Next value :");  
20.                 val2 = int.Parse(ReadLine());  
21.                 WriteLine("Div : {0}", (val1 / val2));  
22.             }  
23.             catch (Exception ex) if (val2 == 0)  
24.             {  
25.                 WriteLine("Can't be Division by zero ☺");  
26.             }  
27.             catch (Exception ex)  
28.             {  
29.                 WriteLine(ex.Message);  
30.             }  
31.             ReadLine();  
32.         }  
33.     }  
34. 

0 comments:

Post a Comment