Flow control in try catch finally in C#

The flow control in try-catch-finally blocks in C# follows a specific execution order depending on whether exceptions occur. Understanding this flow is crucial for proper error handling and resource management in your applications.

Syntax

Following is the syntax for try-catch-finally blocks −

try {
   // Code that may throw an exception
}
catch (ExceptionType e) {
   // Exception handling code
}
finally {
   // Code that always executes
}

Flow Control Rules

  • The try block executes first and contains code that might throw an exception.

  • If an exception occurs, control immediately transfers to the matching catch block.

  • The finally block always executes, regardless of whether an exception occurred.

  • If no exception occurs, the catch block is skipped, but finally still executes.

Try-Catch-Finally Flow Control TRY Block Exception? Thrown? NO YES CATCH Block FINALLY Block Finally block always executes

Example with Exception

The following example demonstrates flow control when an exception occurs −

using System;

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

The output of the above code is −

Executing try block
Executing catch block
Exception caught: Attempted to divide by zero.
Executing finally block
Result: 0

Example without Exception

The following example shows flow control when no exception occurs −

using System;

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

The output of the above code is −

Executing try block
Division completed successfully
Executing finally block
Result: 5

Flow Control Comparison

Scenario Execution Order Catch Block
Exception occurs try ? catch ? finally Executes
No exception try ? finally Skipped
Exception in finally try ? catch ? finally (with new exception) Original exception may be lost

Conclusion

The try-catch-finally flow control in C# ensures that the finally block always executes, making it perfect for cleanup operations. When an exception occurs, control transfers from try to catch, then to finally. Without exceptions, control goes from try directly to finally, skipping the catch block entirely.

Updated on: 2026-03-17T07:04:35+05:30

782 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements