How to Throw an Exception from an Expression with ?? throw in C#
name ?? throw new ArgumentNullException(nameof(name)) uses name when it has a value and throws when it's null, all in one expression. A throw that works inside an expression like this is called a throw expression.
The C# reference: You can also use throw as an expression. This approach might be convenient in several cases, including: the conditional operator, the null-coalescing operator, and an expression-bodied lambda or method.
Syntax
<value> ?? throw new <ExceptionType>(<arguments>)
<condition> ? <value> : throw new <ExceptionType>(<arguments>)
<method signature> => throw new <ExceptionType>(<arguments>);
| Part | What it is |
|---|---|
<value> ?? | The value to use when it isn't null |
<condition> ? <value> : | The value to use when the condition is true |
<method signature> => | An expression-bodied method whose body is the throw expression |
throw new <ExceptionType>(<arguments>) | The exception to throw when the other side isn't used |
The throw side runs only when it's needed. The language specification on ??: The operation evaluates b only if a is null. And once it runs, The behavior at runtime of the evaluation of a throw expression is the same as specified for a throw statement.
The Example
A console app called ThrowExpression has a Greet method that uses ?? throw for a missing name, and a CheckAge method that uses ?: with throw for a negative age. It calls each one with a good value and a bad one, and catches the two exceptions.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n ThrowExpression
cd ThrowExpression
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/ThrowExpression/ThrowExpression.csproj:
Determining projects to restore...
Restored /Users/you/source/ThrowExpression/ThrowExpression.csproj (in 41 ms).
Restore succeeded.
dotnet new console -n ThrowExpression creates a folder named ThrowExpression 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 ThrowExpression.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the calls:
Console.WriteLine(Greet("Asha"));
Console.WriteLine($"Age: {CheckAge(30)}");
try
{
Console.WriteLine(Greet(null));
}
catch (ArgumentNullException exception)
{
Console.WriteLine($"Greet(null) threw: {exception.Message}");
}
try
{
Console.WriteLine(CheckAge(-1));
}
catch (ArgumentOutOfRangeException exception)
{
Console.WriteLine($"CheckAge(-1) threw: {exception.Message}");
}
The first two calls pass good values. The two try blocks pass null and -1, and each catch prints the message of the exception that came out.
Then the two methods:
// ?? throw: use name, or throw when name is null.
static string Greet(string? name)
{
string checkedName = name ?? throw new ArgumentNullException(nameof(name));
return $"Hello, {checkedName}!";
}
// ?: with throw, in an expression-bodied method: the age, or an exception when it's negative.
static int CheckAge(int age) =>
age >= 0 ? age : throw new ArgumentOutOfRangeException(nameof(age), "Age can't be negative.");
name ?? throw new ArgumentNullException(nameof(name))givesnamewhen it isn'tnull. When it is, the right side runs and throws.nameof(name)passes the parameter's name to the exception.age >= 0 ? age : throw new ArgumentOutOfRangeException(...)givesagewhen it's 0 or more, and throws otherwise.static int CheckAge(int age) =>makesCheckAgean expression-bodied method: its whole body is that one expression.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Hello, Asha!
Age: 30
Greet(null) threw: Value cannot be null. (Parameter 'name')
CheckAge(-1) threw: Age can't be negative. (Parameter 'age')

The good values went straight through. null made Greet throw an ArgumentNullException, whose message names the parameter, and -1 made CheckAge throw with its own message, Age can't be negative.
The Complete File

Console.WriteLine(Greet("Asha"));
Console.WriteLine($"Age: {CheckAge(30)}");
try
{
Console.WriteLine(Greet(null));
}
catch (ArgumentNullException exception)
{
Console.WriteLine($"Greet(null) threw: {exception.Message}");
}
try
{
Console.WriteLine(CheckAge(-1));
}
catch (ArgumentOutOfRangeException exception)
{
Console.WriteLine($"CheckAge(-1) threw: {exception.Message}");
}
// ?? throw: use name, or throw when name is null.
static string Greet(string? name)
{
string checkedName = name ?? throw new ArgumentNullException(nameof(name));
return $"Hello, {checkedName}!";
}
// ?: with throw, in an expression-bodied method: the age, or an exception when it's negative.
static int CheckAge(int age) =>
age >= 0 ? age : throw new ArgumentOutOfRangeException(nameof(age), "Age can't be negative.");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing return throw | return throw new ArgumentNullException(nameof(name)); doesn't build: error CS8115: A throw expression is not allowed in this context. | Drop the return. On its own line, throw new ArgumentNullException(nameof(name)); is a throw statement, and it threw the same Value cannot be null. (Parameter 'name') |
?? throw on a value that can't be null | With int count = 3;, count ?? throw new InvalidOperationException("No count.") doesn't build: error CS0019: Operator '??' cannot be applied to operands of type 'int' and '<throw expression>'. The specification, where A is the type of the left side: If A exists and is a non-nullable value type or a pointer type in unsafe code, a compile-time error occurs. | Use ?? throw on something that can be null, such as the string? in Greet. To test a number, use ?: as CheckAge does |
Expecting ?? throw to stop an empty string | Only null throws: Greet("") returned Hello, ! | Check for empty text too: ArgumentException.ThrowIfNullOrWhiteSpace(name) threw The value cannot be an empty string or composed entirely of whitespace. (Parameter 'name') |
FAQ
Can a switch expression arm throw?
Yes. Here the last arm, _, takes every size that the other arms don't list, and throws:
Console.WriteLine(SizeName(2));
try
{
Console.WriteLine(SizeName(9));
}
catch (ArgumentOutOfRangeException exception)
{
Console.WriteLine($"SizeName(9) threw: {exception.Message}");
}
static string SizeName(int size) => size switch
{
1 => "Small",
2 => "Medium",
3 => "Large",
_ => throw new ArgumentOutOfRangeException(nameof(size), "Pick 1, 2 or 3."),
};
Medium
SizeName(9) threw: Pick 1, 2 or 3. (Parameter 'size')
Conclusion
Use a throw expression to check a value right where you use it: value ?? throw new ... for null, condition ? value : throw new ... for any other test, and => throw new ... for a method that only throws. Where the check is a whole step of its own, a throw statement on its own line does the same job.
Additional Resources
- The throw expression — where throw works as an expression.
- The ?? and ??= operators — the null-coalescing operator, with a throw expression on its right.
- ArgumentException.ThrowIfNullOrWhiteSpace — a check for null, empty and whitespace-only strings.
