How to Rethrow an Exception with throw; in C#
throw; on its own, inside a catch block, throws the exception that the block caught once more. It keeps the exception's stack trace, so the code that catches it next still sees the method and the line where it started.
The C# reference: Inside a catch block, use a throw; statement to re-throw the exception that the catch block handles. That lets a method do what it can, such as writing a line to a log, and still leave the problem to the code that called it.
What throw; keeps is the stack trace. The reference for Exception.StackTrace explains the term: A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. Code analysis rule CA2200 says how to keep it: To keep the original stack trace information with the exception, use the throw statement without specifying the exception.
Syntax
try
{
<code that can throw>
}
catch (<ExceptionType>)
{
<what this method can do>
throw;
}
| Part | What it is |
|---|---|
<code that can throw> | The code that might throw, such as a call to another method |
catch (<ExceptionType>) | The clause that catches the exception, such as catch (FormatException). It can also name the exception, such as catch (FormatException e), when the block needs to read it |
<what this method can do> | Optional: code that runs before the exception moves on, such as a line in a log |
throw; | The keyword throw with nothing after it. It throws the caught exception once more, so code outside this try statement, such as the method that called this one, can catch it |
The Example
A console app called RethrowException reads a temperature typed as text. Main calls Thermometer.Read, which calls Thermometer.Parse to turn the text into an int. The text is twenty, in words, so parsing it throws a FormatException. Read catches it, prints a line and rethrows it with throw;, and Main catches it once more and prints its stack trace.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n RethrowException
cd RethrowException
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/RethrowException/RethrowException.csproj:
Determining projects to restore...
Restored /Users/you/source/RethrowException/RethrowException.csproj (in 46 ms).
Restore succeeded.
dotnet new console -n RethrowException creates a folder named RethrowException 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 RethrowException.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the code at the top of the file:
// Main calls Thermometer.Read, which calls Thermometer.Parse.
try
{
int degrees = Thermometer.Read("twenty");
Console.WriteLine($"Temperature: {degrees}");
}
catch (FormatException e)
{
Console.WriteLine($"Main caught: {e.Message}");
Console.WriteLine("Stack trace:");
Console.WriteLine(e.StackTrace);
}
This is the code at the top of Program.cs, which runs first and which the stack trace calls Main. It calls Thermometer.Read inside a try block. Its catch block prints the exception's message, then e.StackTrace, the stack trace as text, with one line for each method.
Then the class, with the method that rethrows:
static class Thermometer
{
public static int Read(string text)
{
try
{
return Parse(text);
}
catch (FormatException)
{
// Do what this method can, then pass the same exception on.
Console.WriteLine($"Read could not use '{text}', rethrowing");
throw;
}
}
Read calls Parse inside its own try block. When Parse throws a FormatException, the catch block prints a line, and throw; throws the same exception once more, out of Read and into the try block in Main. The clause is catch (FormatException), with no name for the exception, because throw; doesn't need one.
Last, the method where the exception starts:
private static int Parse(string text)
{
return int.Parse(text);
}
}
int.Parse(text) turns text into an int, and throws a FormatException when the text isn't a whole number. private means only code inside Thermometer can call Parse.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Read could not use 'twenty', rethrowing
Main caught: The input string 'twenty' was not in a correct format.
Stack trace:
at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
at System.Int32.Parse(String s)
at Thermometer.Parse(String text) in /Users/you/source/RethrowException/Program.cs:line 32
at Thermometer.Read(String text) in /Users/you/source/RethrowException/Program.cs:line 20
at Program.<Main>$(String[] args) in /Users/you/source/RethrowException/Program.cs:line 4

Read printed its line first, then Main caught the same FormatException. Read the stack trace from the top down. The exception started inside .NET's own code for int.Parse: the two lines without a file. Thermometer.Parse called it at line 32, Thermometer.Read called Parse at line 20, and the top of Program.cs, shown as Program.<Main>$, called Read at line 4. The rethrow in Read hid none of it: the trace still names Parse, the method that failed, and its line.
The Complete File

// Main calls Thermometer.Read, which calls Thermometer.Parse.
try
{
int degrees = Thermometer.Read("twenty");
Console.WriteLine($"Temperature: {degrees}");
}
catch (FormatException e)
{
Console.WriteLine($"Main caught: {e.Message}");
Console.WriteLine("Stack trace:");
Console.WriteLine(e.StackTrace);
}
static class Thermometer
{
public static int Read(string text)
{
try
{
return Parse(text);
}
catch (FormatException)
{
// Do what this method can, then pass the same exception on.
Console.WriteLine($"Read could not use '{text}', rethrowing");
throw;
}
}
private static int Parse(string text)
{
return int.Parse(text);
}
}
What throw e; Does to the Stack Trace
throw e; names the exception, and that changes its stack trace. Here is Read with catch (FormatException e) and throw e; in place of throw;, in a copy of the program saved as checks/c550a-throw-e.cs:
public static int Read(string text)
{
try
{
return Parse(text);
}
catch (FormatException e)
{
Console.WriteLine($"Read could not use '{text}', rethrowing");
throw e;
}
}
The build gave a warning, and the run printed a much shorter trace:
/Users/you/source/checks/c550a-throw-e.cs(25,13): warning CA2200: Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)
Read could not use 'twenty', rethrowing
Main caught: The input string 'twenty' was not in a correct format.
Stack trace:
at Thermometer.Read(String text) in /Users/you/source/checks/c550a-throw-e.cs:line 25
at Program.<Main>$(String[] args) in /Users/you/source/checks/c550a-throw-e.cs:line 4
The trace now starts at Thermometer.Read, line 25, the throw e; line, and the lines for Parse and int.Parse are gone. CA2200 explains why: If an exception is rethrown by specifying the exception in the throw statement, the stack trace is restarted at the current method and the list of method calls between the original method that threw the exception and the current method is lost. In a bigger program, the lost lines are the ones that say which method really failed.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Rethrowing with throw e; | It built with warning CA2200: Re-throwing caught exception changes stack information, and the trace lost the lines for Parse and int.Parse, as the section above shows | Write throw; with nothing after it. CA2200 is clear about the warning: Do not suppress a warning from this rule. |
Writing throw; outside a catch block | throw; in an if block didn't build: error CS0156: A throw statement with no arguments is not allowed outside of a catch clause | Use throw; only inside a catch block, where there's a caught exception to pass on |
Writing code after throw; | A Console.WriteLine below throw; built with warning CS0162: Unreachable code detected, and the run printed only Main caught: The input string 'twenty' was not in a correct format. The line never ran. The specification: Because a throw statement unconditionally transfers control elsewhere, the end point of a throw statement is never reachable. | Put everything the catch block has to do before throw;, as the example does |
FAQ
Is the rethrown exception the same object?
Yes. The C# specification: A throw statement with no expression can be used only in a catch block, in which case, that statement re-throws the exception that is currently being handled by that catch block. Here Read keeps the exception it caught in a field, LastCaught, before throw;, and Main compares it with the one it caught, using ReferenceEquals. Its reference: Determines whether the specified Object instances are the same instance.
try
{
Thermometer.Read("twenty");
}
catch (FormatException e)
{
Console.WriteLine($"Same exception object: {ReferenceEquals(e, Thermometer.LastCaught)}");
}
static class Thermometer
{
public static FormatException? LastCaught;
public static int Read(string text)
{
try
{
return int.Parse(text);
}
catch (FormatException e)
{
LastCaught = e;
throw;
}
}
}
Same exception object: True
When should a method rethrow?
When it can do part of the work but can't fix the problem. The C# guide lists it as one reason to catch an exception: You want to partially handle an exception before passing it on for more handling. In the example, Read can report the text it couldn't use, but only Main can decide what to do without a temperature. A method with nothing to add needs no try at all: Parse has none, and the exception still reached Main with Parse in its trace.
Conclusion
Inside a catch block, throw; passes the exception you caught on, unchanged, with a stack trace that still starts where the exception was thrown. Do what the method can first, such as logging, then rethrow. Don't write throw e;, which restarts the trace at the current method, and keep throw; inside catch blocks, the only place it builds.
Additional Resources
- Exception-handling statements —
throw,tryandcatch, and howthrow;rethrows. - CA2200: Rethrow to preserve stack details — what
throw e;does to the stack trace. Exception.StackTrace— the property that holds the stack trace.- Exception handling (C# programming guide) — when to catch an exception, and when to pass it on.
- The throw statement (C# language specification) — where
throw;with no expression is allowed.
