Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
tryblock executes first and contains code that might throw an exception. -
If an exception occurs, control immediately transfers to the matching
catchblock. -
The
finallyblock always executes, regardless of whether an exception occurred. -
If no exception occurs, the
catchblock is skipped, butfinallystill 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.
