Flow control in try catch finally in C#

CsharpProgrammingServer Side Programming

The flow control in try, catch, and finally can be understood using the following example. Here, we are dividing two numbers −


 Live Demo

using System;

namespace ErrorHandlingApplication {
   class DivNumbers {
      int result;
      DivNumbers() {
         result = 0;
      public void division(int num1, int num2) {
         try {
            result = num1 / num2;
         } catch (DivideByZeroException e) {
            Console.WriteLine("Exception caught: {0}", e);
         } finally {
            Console.WriteLine("Result: {0}", result);
      static void Main(string[] args) {
         DivNumbers d = new DivNumbers();
         d.division(25, 0);


Exception caught: System.DivideByZeroException: Attempted to divide by zero.
at ErrorHandlingApplication.DivNumbers.division (System.Int32 num1, System.Int32 num2) [0x00000] in :0
Result: 0

The following shows the flow control in exceptions handling using try catch finally in C#

  • If an exception occurs in try block, then the control transfers to the catch block.
  • After the catch block finish, then the finally block works.
  • If no exception occurs, then firstly the try occurs and then the flow control to finally block
Updated on 22-Jun-2020 10:58:29