The rethrow statement in C# is a control flow mechanism used exclusively within aDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
catch block to propagate the currently handled exception up the call stack. It is invoked using the throw keyword without an accompanying exception operand and compiles to the rethrow Common Intermediate Language (CIL) instruction.
Syntax
Mechanics and Behavior
When the Common Language Runtime (CLR) encounters athrow; statement, it terminates the execution of the current catch block and resumes the exception propagation process. The CLR searches the call stack for the next enclosing catch block that matches the exception type.
Stack Trace Modification
While the rethrow statement preserves the historical call stack (unlike explicitly throwing the caught exception variable), it does not leave theException.StackTrace completely unmodified.
throw;(Rethrow): Preserves the previous stack frames, but the CLR updates the current method’s stack frame to reflect the line number of thethrow;statement. If the exception was instantiated and originally thrown within the same method, the original line number in thetryblock is permanently lost and replaced by the line number of the rethrow statement in thecatchblock.throw ex;(Throwing the caught variable): Compiles to the standardthrowCIL instruction. This resets theStackTraceproperty entirely. The CLR records the line containingthrow ex;as the absolute origin of the exception, permanently destroying all historical call stack data from previous frames.
Code Comparison
Compiler Constraints
- Context Restriction: The parameterless
throw;statement is only valid lexically inside acatchblock. Attempting to use it outside of acatchblock results in compiler error CS0156 (“A throw statement with no arguments is not allowed outside of a catch clause”). - Anonymous and Local Functions: Even when lexically nested entirely inside a
catchblock,throw;cannot be used within lambdas, anonymous methods, or local functions. Doing so violates the context restriction and results in compiler error CS0156. - Finally Blocks: The statement cannot be used inside a
finallyblock that is a sibling to thecatchblock. However, it is permitted inside afinallyblock that is strictly nested within acatchblock (e.g.,catch { try { } finally { throw; } }). - Standard Nested Blocks: It can be used within standard nested control structures (such as
ifstatements,switchstatements, orforloops) provided those structures are directly within the lexical scope of thecatchclause and do not cross into a separate function boundary.
Master C# with Deep Grasping Methodology!Learn More





