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
Chained Exceptions in C#
Chained exceptions in C# allow you to preserve the original exception information while throwing a new exception. This creates a chain of exceptions where each exception wraps the previous one, maintaining the complete error context throughout the call stack.
When an exception occurs deep in your application, chained exceptions help maintain the full error history by wrapping the original exception inside a new one using the innerException parameter of the Exception constructor.
Syntax
Following is the syntax for creating a chained exception −
try {
// code that might throw exception
} catch (Exception originalException) {
throw new Exception("New exception message", originalException);
}
The innerException parameter preserves the original exception details while adding context with a new message.
Complete Example
using System;
class Demo {
static void Main(string[] args) {
try {
One();
} catch (Exception e) {
Console.WriteLine("Caught Exception:");
Console.WriteLine(e);
}
}
static void One() {
try {
Two();
} catch (Exception e) {
throw new Exception("First exception!", e);
}
}
static void Two() {
try {
Three();
} catch (Exception e) {
throw new Exception("Second Exception!", e);
}
}
static void Three() {
try {
Last();
} catch (Exception e) {
throw new Exception("Third Exception!", e);
}
}
static void Last() {
throw new Exception("Last exception!");
}
}
The output of the above code is −
Caught Exception: System.Exception: First exception! ---> System.Exception: Second Exception! ---> System.Exception: Third Exception! ---> System.Exception: Last exception! at Demo.Last() in Program.cs:line 36 at Demo.Three() in Program.cs:line 30 --- End of inner exception stack trace --- at Demo.Three() in Program.cs:line 32 at Demo.Two() in Program.cs:line 24 --- End of inner exception stack trace --- at Demo.Two() in Program.cs:line 26 at Demo.One() in Program.cs:line 18 --- End of inner exception stack trace --- at Demo.One() in Program.cs:line 20 at Demo.Main(String[] args) in Program.cs:line 9
Accessing Inner Exceptions
You can traverse the exception chain using the InnerException property −
using System;
class ExceptionTraversal {
static void Main(string[] args) {
try {
ThrowChainedException();
} catch (Exception ex) {
Console.WriteLine("Exception Chain:");
Exception current = ex;
int level = 1;
while (current != null) {
Console.WriteLine($"Level {level}: {current.Message}");
current = current.InnerException;
level++;
}
}
}
static void ThrowChainedException() {
try {
throw new InvalidOperationException("Original error");
} catch (Exception e) {
throw new ApplicationException("Wrapped error", e);
}
}
}
The output of the above code is −
Exception Chain: Level 1: Wrapped error Level 2: Original error
Common Use Cases
-
Adding context − Wrap low-level exceptions with business-meaningful messages.
-
Layer abstraction − Convert technical exceptions to user-friendly ones while preserving details.
-
Debugging support − Maintain complete stack trace information for troubleshooting.
-
Error aggregation − Collect multiple related errors into a single exception chain.
Conclusion
Chained exceptions in C# preserve the complete error context by wrapping original exceptions within new ones. This technique maintains the full stack trace while allowing you to add meaningful context at each application layer, making debugging and error handling more effective.
