Can Multiple Catch Blocks be executed in C#?

Category: C SharpCan Multiple Catch Blocks be executed in C#?
Editor">Editor Staff asked 2 years ago

Can Multiple Catch Blocks be executed in C#? 

1 Answers
Editor">Editor Staff answered 2 years ago

We can use multiple catch blocks with a try statement. Each catch block can catch a different exception. The following code example shows how to implement multiple catch statements with a single try statement.

 

  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. }