Throwing and rethrowing help you handle errors in your program. You can stop the program when something goes wrong or pass the error up to fix it later.
Throw and rethrow patterns in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
throw new Exception("Error message"); // To rethrow an existing exception inside a catch block: throw; // Or to throw a new exception with the caught one as inner: throw new Exception("New message", ex);
throw creates a new error or passes an existing one.
Use throw; alone inside a catch to keep the original error details.
Examples
C Sharp (C#)
throw new Exception("Something went wrong");
C Sharp (C#)
try { // code that may fail } catch (Exception ex) { throw; // rethrows the same error }
C Sharp (C#)
try { // code that may fail } catch (Exception ex) { throw new Exception("New error", ex); // wraps original error }
Sample Program
This program shows how to throw a new error with an original error inside, and how to rethrow an error to keep its details.
C Sharp (C#)
using System; class Program { static void Main() { try { CauseError(); } catch (Exception ex) { Console.WriteLine("Caught in Main: " + ex.Message); throw; // rethrow to keep original error } } static void CauseError() { try { throw new Exception("Original error"); } catch (Exception ex) { Console.WriteLine("Caught in CauseError: " + ex.Message); throw new Exception("Wrapped error", ex); // throw new with inner } } }
Important Notes
Use throw; alone to keep the original stack trace.
Throwing a new exception with the old one inside helps add context.
Rethrowing without losing details helps debugging later.
Summary
Throw stops the program with an error.
Rethrow passes the caught error up without changing it.
You can wrap errors to add more information.
Practice
1. What does the
throw statement do in C# exception handling?easy
Solution
Step 1: Understand the purpose of
Thethrowthrowstatement is used to signal that an error has occurred and to stop normal program flow.Step 2: Recognize program behavior on
Whenthrowthrowis executed, the program stops and looks for a matchingcatchblock or terminates if none is found.Final Answer:
It stops the program and signals an error. -> Option CQuick Check:
throwstops program = C [OK]
Hint: Throw always stops execution and signals an error [OK]
Common Mistakes:
- Thinking throw continues program normally
- Confusing throw with logging
- Assuming throw fixes errors automatically
2. Which of the following is the correct syntax to rethrow an exception in a
catch block without changing it?easy
Solution
Step 1: Identify rethrow syntax
To rethrow the caught exception preserving the original stack trace, usethrow;without specifying the exception variable.Step 2: Understand difference from
throw ex;throw ex;resets the stack trace, which is not a pure rethrow.Final Answer:
throw; -> Option DQuick Check:
Rethrow syntax = throw; [OK]
Hint: Use plain 'throw;' to rethrow without losing stack trace [OK]
Common Mistakes:
- Using 'throw ex;' which resets stack trace
- Trying to throw a new exception instead
- Using invalid syntax like 'throw catch;'
3. What will be the output of the following C# code?
try {
throw new Exception("Error 1");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
throw;
}medium
Solution
Step 1: Analyze the try block
The try block throws an exception with message "Error 1".Step 2: Analyze the catch block
The catch block prints the exception message, then rethrows the same exception usingthrow;.Step 3: Understand program flow after rethrow
Since the exception is rethrown and not caught again, the program terminates with the same exception after printing.Final Answer:
Error 1 printed, then program terminates with the same exception. -> Option AQuick Check:
Print then rethrow = B [OK]
Hint: Rethrow after print causes termination with printed message [OK]
Common Mistakes:
- Assuming program continues after rethrow
- Thinking no output is printed
- Confusing rethrow with new exception creation
4. Identify the error in this code snippet:
try {
// some code
} catch (Exception ex) {
throw ex;
}medium
Solution
Step 1: Understand
Usingthrow ex;effectthrow ex;throws the exception but resets the stack trace, losing original error location.Step 2: Identify correct rethrow method
To preserve stack trace, usethrow;without specifying the exception variable.Final Answer:
Usingthrow ex;resets the stack trace, losing original error info. -> Option BQuick Check:
Throwing exception variable resets stack trace = D [OK]
Hint: Use 'throw;' not 'throw ex;' to keep original stack trace [OK]
Common Mistakes:
- Thinking 'throw ex;' preserves stack trace
- Believing finally block is mandatory
- Assuming Exception cannot be caught directly
5. You want to catch an exception, add extra info, and then rethrow it preserving the original stack trace. Which pattern is correct?
try {
// code
} catch (Exception ex) {
// add info
???
}hard
Solution
Step 1: Understand wrapping exceptions
To add extra info, create a new exception with the original as inner exception:new Exception("Extra info", ex).Step 2: Preserve original stack trace
This wrapping keeps original exception details inside the new one, preserving context.Step 3: Why not other options?
throw ex;resets stack trace,throw;rethrows original without extra info, andthrow new Exception(ex.Message);loses original exception object.Final Answer:
throw new Exception("Extra info", ex); -> Option AQuick Check:
Wrap with new Exception and inner ex = A [OK]
Hint: Wrap original in new Exception to add info and preserve trace [OK]
Common Mistakes:
- Using 'throw ex;' which loses stack trace
- Using 'throw;' which loses added info
- Creating new Exception without inner exception
