Saturday, May 5, 2018

C# : Can Multiple Catch Blocks executed in c#

we can use multiple Catches block with every try but when any Exceptions is throw by debugger so every catches match this exception type with their signature and catch the exception by any single catch block so that means we can use multiple catches blocks but only one can executed at once like:
  1. using System;  
  2. class MyClient {  
  3.     public static void Main() {  
  4.         int x = 0;  
  5.         int div = 0;  
  6.         try {  
  7.             div = 100 / x;  
  8.             Console.WriteLine("Not executed line");  
  9.         } catch (DivideByZeroException de) {  
  10.             Console.WriteLine("DivideByZeroException");  
  11.         } catch (Exception ee) {  
  12.             Console.WriteLine("Exception");  
  13.         } finally {  
  14.             Console.WriteLine("Finally Block");  
  15.         }  
  16.         Console.WriteLine("Result is {0}", div);  
  17.     }  
  18. }  

No comments:

Post a Comment