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
Exception Propagation in C#
Exception propagation in C# refers to the process of how exceptions move up through the call stack when they are not handled by the current method or try-catch block. When an exception occurs, the runtime searches for an appropriate exception handler, and if none is found, the exception propagates to the calling method.
How Exception Propagation Works
When an exception occurs in a try block, the runtime checks the corresponding catch blocks to see if they can handle the exception. If no matching catch block is found, the exception propagates to a higher-level try block or to the calling method. This process continues until the exception is caught or the program terminates.
Example with Nested Try Statements
The following example demonstrates exception propagation using nested try-catch blocks −
using System;
public class Demo {
public static void Main() {
try {
try {
throw new ArgumentException();
} catch (NullReferenceException e) {
Console.WriteLine("catch one");
} finally {
Console.WriteLine("finally one");
}
} catch (Exception e) {
Console.WriteLine("catch two");
} finally {
Console.WriteLine("finally two");
}
}
}
The output of the above code is −
finally one catch two finally two
In this example, the inner try block throws an ArgumentException, but the inner catch block only handles NullReferenceException. Since there's no match, the exception propagates to the outer catch block, which handles the general Exception type.
Exception Propagation Across Methods
Exception propagation also works across method calls. If a method doesn't handle an exception, it propagates to the calling method −
using System;
public class PropagationExample {
public static void Main() {
try {
MethodA();
} catch (DivideByZeroException e) {
Console.WriteLine("Exception caught in Main: " + e.Message);
}
}
public static void MethodA() {
Console.WriteLine("In Method A");
MethodB();
}
public static void MethodB() {
Console.WriteLine("In Method B");
int result = 10 / 0; // This will throw DivideByZeroException
}
}
The output of the above code is −
In Method A In Method B Exception caught in Main: Attempted to divide by zero.
Key Points About Exception Propagation
-
Finally blocks always execute during propagation, regardless of whether the exception is caught.
-
If no appropriate catch block is found anywhere in the call stack, the program terminates with an unhandled exception.
-
Exception propagation follows the call stack hierarchy from inner to outer scopes.
-
Once an exception is caught and handled, propagation stops unless a new exception is thrown.
Conclusion
Exception propagation in C# ensures that exceptions flow up through the call stack until they find an appropriate handler. This mechanism allows for flexible error handling by letting specific exceptions be handled at the most appropriate level in your application's architecture.
