How to Wrap an Exception in Another with an Inner Exception in C#
throw new ArgumentException(message, e) inside a catch block throws a new exception with your own message and keeps the exception you caught, e, in its InnerException property. The code that catches the new one reads what went wrong in your words and can still see the cause.
Microsoft's guide to exceptions explains why you'd do this: To improve the caller's ability to determine the reason an exception is thrown, it is sometimes desirable for a method to catch an exception thrown by a helper routine and then throw an exception more indicative of the error that has occurred. The new exception keeps the old one inside it, as its inner exception. The reference for InnerException: Gets the Exception instance that caused the current exception.
You hand the old exception over when you create the new one. Exception types such as ArgumentException and InvalidOperationException have a constructor for that, and the one on Exception itself Initializes a new instance of the Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.
Syntax
try
{
<code that can throw>
}
catch (<CaughtType> <e>)
{
throw new <NewType>(<message>, <e>);
}
| Part | What it is |
|---|---|
<CaughtType> | The type of exception you catch, such as FormatException |
<e> | A name for the caught exception. Passed as the second argument, it becomes the new exception's InnerException |
<NewType> | The exception to throw instead, such as ArgumentException. It needs a constructor that takes a message and an inner exception |
<message> | Text that says what went wrong, in words that make sense to the code that called this method |
The Example
A console app called InnerExceptionWrap reads the quantity a customer typed in an order form: three, in words. Order.ReadQuantity calls int.Parse, which throws a FormatException. ReadQuantity catches it and throws an ArgumentException with its own message and the FormatException inside. The code at the top of the file catches the ArgumentException and prints both exceptions.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n InnerExceptionWrap
cd InnerExceptionWrap
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/InnerExceptionWrap/InnerExceptionWrap.csproj:
Determining projects to restore...
Restored /Users/you/source/InnerExceptionWrap/InnerExceptionWrap.csproj (in 46 ms).
Restore succeeded.
dotnet new console -n InnerExceptionWrap creates a folder named InnerExceptionWrap with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or InnerExceptionWrap.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the code at the top of the file:
// A customer typed "three" in the quantity box of an order form.
try
{
int quantity = Order.ReadQuantity("three");
Console.WriteLine($"Quantity: {quantity}");
}
catch (ArgumentException e)
{
// The new exception, in the words of ReadQuantity.
Console.WriteLine($"Caught: {e.GetType().Name}: {e.Message}");
// The exception that caused it.
if (e.InnerException is not null)
{
Console.WriteLine($"Inner: {e.InnerException.GetType().Name}: {e.InnerException.Message}");
}
}
It calls Order.ReadQuantity and catches an ArgumentException, the type that ReadQuantity throws. GetType().Name gives the name of an exception's type, and Message its message. InnerException is null when there's no inner exception, so the if reads it only when it's there.
Then the class that wraps the exception:
static class Order
{
public static int ReadQuantity(string text)
{
try
{
return int.Parse(text);
}
catch (FormatException e)
{
// Say what went wrong in this method's words, and pass the cause along.
throw new ArgumentException($"The quantity '{text}' must be a whole number.", e);
}
}
}
int.Parse(text) throws a FormatException when the text isn't a whole number. The catch block names it e and passes it as the second argument of new ArgumentException(...). The first argument is the new message, about the quantity, which is what ReadQuantity knows about.
The new exception is an ArgumentException because the text that ReadQuantity was given is the problem. Microsoft's guide to creating exceptions names this case, an argument that causes an exception: the original exception should be caught and an ArgumentException instance should be created. The original exception should be passed to the constructor of the ArgumentException as the InnerException parameter.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Caught: ArgumentException: The quantity 'three' must be a whole number.
Inner: FormatException: The input string 'three' was not in a correct format.

The code at the top caught an ArgumentException with the message from ReadQuantity. Its InnerException was the FormatException that int.Parse threw, with its own message. Both are there: what went wrong, in the words of the method that knows about quantities, and the cause underneath it. The reference for InnerException describes this job: When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.
The Complete File

// A customer typed "three" in the quantity box of an order form.
try
{
int quantity = Order.ReadQuantity("three");
Console.WriteLine($"Quantity: {quantity}");
}
catch (ArgumentException e)
{
// The new exception, in the words of ReadQuantity.
Console.WriteLine($"Caught: {e.GetType().Name}: {e.Message}");
// The exception that caused it.
if (e.InnerException is not null)
{
Console.WriteLine($"Inner: {e.InnerException.GetType().Name}: {e.InnerException.Message}");
}
}
static class Order
{
public static int ReadQuantity(string text)
{
try
{
return int.Parse(text);
}
catch (FormatException e)
{
// Say what went wrong in this method's words, and pass the cause along.
throw new ArgumentException($"The quantity '{text}' must be a whole number.", e);
}
}
}
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the caught exception | Without e as the second argument, throw new ArgumentException(...) built with warning CS0168: The variable 'e' is declared but never used, and the caller printed Inner exception is null: True. The cause was gone | Pass the caught exception as the second argument: new ArgumentException(message, e) |
Setting InnerException after creating the exception | problem.InnerException = e; didn't build: error CS0200: Property or indexer 'Exception.InnerException' cannot be assigned to -- it is read only | Pass it to the constructor. The C# guide: Because the InnerException property is read-only, you must assign it in the constructor of the new exception. |
| Catching the inner exception's type | A catch (FormatException e) at the top didn't run, because the exception that reached it was an ArgumentException. The program stopped with Unhandled exception. System.ArgumentException: The quantity 'three' must be a whole number. | Catch the type that was thrown, ArgumentException, and read InnerException for the cause, as the example does |
FAQ
What does the whole exception look like with its inner exception?
ToString() writes out the chain. The reference: The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. A copy of the program, saved as checks/c551d-tostring.cs, with Console.WriteLine(e.ToString()); in its catch block, printed:
System.ArgumentException: The quantity 'three' must be a whole number.
---> System.FormatException: The input string 'three' was not in a correct format.
at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
at System.Int32.Parse(String s)
at Order.ReadQuantity(String text) in /Users/you/source/checks/c551d-tostring.cs:line 17
--- End of inner exception stack trace ---
at Order.ReadQuantity(String text) in /Users/you/source/checks/c551d-tostring.cs:line 21
at Program.<Main>$(String[] args) in /Users/you/source/checks/c551d-tostring.cs:line 4
---> starts the inner exception. --- End of inner exception stack trace --- marks where its stack trace ends and the stack trace of the new exception begins.
What if the exception is wrapped more than once?
Each exception can have an inner exception of its own, so they form a chain, and GetBaseException() returns the first one. The reference: The first exception thrown in a chain of exceptions. Here a PlaceOrder method calls ReadQuantity, with Order as in the example, and wraps its ArgumentException in an InvalidOperationException:
try
{
Shop.PlaceOrder("three");
}
catch (InvalidOperationException e)
{
Console.WriteLine($"Caught: {e.GetType().Name}: {e.Message}");
if (e.InnerException is not null)
{
Console.WriteLine($"Inner: {e.InnerException.GetType().Name}: {e.InnerException.Message}");
}
// The first exception in the chain.
Exception first = e.GetBaseException();
Console.WriteLine($"Base: {first.GetType().Name}: {first.Message}");
}
static class Shop
{
public static void PlaceOrder(string quantityText)
{
try
{
int quantity = Order.ReadQuantity(quantityText);
Console.WriteLine($"Ordered: {quantity}");
}
catch (ArgumentException e)
{
throw new InvalidOperationException("The order could not be placed.", e);
}
}
}
Caught: InvalidOperationException: The order could not be placed.
Inner: ArgumentException: The quantity 'three' must be a whole number.
Base: FormatException: The input string 'three' was not in a correct format.
InnerException went one level down, to the ArgumentException. GetBaseException() went to the bottom, to the FormatException that int.Parse threw.
Conclusion
To keep the cause when you throw a new exception, catch it with a name and pass it as the second argument: throw new ArgumentException(message, e). The caller catches the new type, reads your message, and finds the original exception in InnerException, or the first one of a longer chain with GetBaseException(). ToString() prints them all.
Additional Resources
Exception.InnerException— the property that holds the cause.Exceptionconstructors — the one that takes a message and an inner exception.- Create and throw exceptions — when to wrap an exception in an
ArgumentException. - Exception class and properties — why a method throws a more meaningful exception.
- How to handle an exception using try/catch — catching one exception and throwing a more fitting one.
Exception.GetBaseException— the first exception in a chain.
