Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The throw statement explicitly signals the occurrence of an anomalous execution condition (an exception) at runtime. When executed, it immediately halts the normal sequential flow of the program and initiates the Common Language Runtime (CLR) two-pass exception handling process, ultimately transferring control to the nearest dynamically enclosing catch clause that matches the thrown exception type.

Syntax

throw [expression];
The expression must evaluate to an object that derives from the System.Exception base class. If the expression evaluates to null, the CLR automatically throws a NullReferenceException instead.

Execution Mechanics

The .NET exception handling mechanism operates using a two-pass model:
  1. Evaluation: The exception expression is evaluated to produce an exception object.
  2. Stack Trace Generation: The CLR populates the StackTrace property of the exception object with the current execution point.
  3. Pass 1 (Searching): The runtime traverses the call stack looking for a try block with a compatible catch clause. During this pass, any exception filters (when clauses) are evaluated.
  4. Pass 2 (Stack Unwinding): Once a matching catch clause is identified, the runtime unwinds the stack from the point of the exception to the target catch block. This phase executes all finally blocks located in the scopes between the throw point and the catch point.
  5. Termination: If the first pass reaches the top of the call stack without finding a matching catch block, the process is terminated via the unhandled exception mechanism, and the second pass does not occur.

Forms of the Throw Statement

1. Throwing a New Exception

This is the standard form where a new exception instance is created and thrown.
throw new ArgumentOutOfRangeException(nameof(index));

2. The Rethrow Statement (throw;)

When used inside a catch block, throw can be used without an expression. This is known as a rethrow.
try
{
    // Code that may fault
}
catch (InvalidOperationException)
{
    throw; 
}
Mechanically, throw; propagates the caught exception while preserving its original StackTrace. This behaves differently than throw ex;, which re-throws the exception but mutates the exception object by resetting the StackTrace to the line containing the throw ex; statement.

3. Throw Expressions

Introduced in C# 7.0, throw can be evaluated as an expression rather than a statement. Because a throw expression never successfully returns a value, its return type is conceptually the bottom type, allowing it to implicitly convert to any type required by the surrounding context. Throw expressions are permitted in the following syntactic contexts: Null-Coalescing Operator (??)
string validString = inputString ?? throw new ArgumentNullException(nameof(inputString));
Conditional Operator (?:)
int result = isValid ? 100 : throw new InvalidOperationException();
Expression-Bodied Members (=>)
public DateTime DateOfBirth => throw new NotImplementedException();
Switch Expression Arms (Introduced in C# 8.0)
string state = input switch
{
    0 => "Idle",
    1 => "Active",
    _ => throw new ArgumentOutOfRangeException(nameof(input))
};
Master C# with Deep Grasping Methodology!Learn More