Pitcher is a utility library to simplify throwing exceptions, especially when checking arguments. It makes methods easier to inline, by reducing code size.
.NET Framework | .NET Core |
---|---|
✔️ | ✔️ |
The Pitcher NuGet package is the prefered way to install and use it.
install-package Pitcher
To use Pitcher, add a using to your source file:
using Pitcher;
Then, you can use Throw
or Throw<T>
to throw exceptions.
Throw an exception directly:
Throw.This(new ArgumentNullException(nameof(args));
Throw an exception based on a condition:
Throw.When(args == null, new ArgumentNullException(nameof(args)); // This will always allocate the exception
Throw an exception based on a condition, with a Func<T>
to create the exception in a more complex way:
Throw.When(args == null, () => new ArgumentNullException(nameof(args));
There are helpers for simplifying argument checking and throwing an ArgumentNullException
.
// Throw ArgumentNullException for parameter
Throw.ArgumentNull.For(nameof(args));
// Throw ArgumentNullException for parameter, with message
Throw.ArgumentNull.For(nameof(args), "Args is a required parameter");
// Throw ArgumentNullException for parameter, when a condition is met
Throw.ArgumentNull.When(args == null, nameof(args));
// Throw ArgumentNullException for parameter with a message, when a condition is met
Throw.ArgumentNull.When(args == null, nameof(args), "Args is a required parameter");
// Throw ArgumentNullException for parameter, when the parameter is null
Throw.ArgumentNull.WhenNull(args, nameof(args));
// Throw ArgumentNullException for parameter with a message, when the parameter is null
Throw.ArgumentNull.WhenNull(args, nameof(args), "Args is a required parameter");
// Throw ArgumentNullException for a null or empty string
Throw.ArgumentNull.WhenNullOrEmpty(args, nameof(args));
// Throw ArgumentNullException with a message for a null or empty string
Throw.ArgumentNull.WhenNullOrEmpty(args, nameof(args), "Args is a required parameter");
// Throw ArgumentNullException for a null, empty or whitespace string
Throw.ArgumentNull.WhenNullOrWhitespace(args, nameof(args));
// Throw ArgumentNullException with a message for a null, empty or whitespace string
Throw.ArgumentNull.WhenNullOrWhitespace(args, nameof(args), "Args is a required parameter");
// Throw ArgumentNullException for a null or empty IEnumerable<T>
Throw.ArgumentNull.WhenNullOrEmpty(new List<string>(), nameof(args));
// Throw ArgumentNullException with a message for a null or empty IEnumerable<T>
Throw.ArgumentNull.WhenNullOrEmpty(new List<string>(), nameof(args), "Args is a required parameter");
Specific helpers are available for throwing an ArgumentOutOfRangeException
.
// Throw ArgumentOutOfRangeException for parameter
Throw.ArgumentOutOfRange.For(nameof(args));
// Throw ArgumentOutOfRangeException for parameter, with message
Throw.ArgumentOutOfRange.For(nameof(args), "Args is out of the acceptable range");
// Throw ArgumentOutOfRangeException for parameter, when a condition is met
Throw.ArgumentOutOfRange.When(args < 0, nameof(args));
// Throw ArgumentOutOfRangeException for parameter with a message, when a condition is met
Throw.ArgumentOutOfRange.When(args < 0, nameof(args), "Args is out of the acceptable range");
For exceptions without parameters or when you don't care about the parameters, you can use Throw<T>
.
// Throw an exception
Throw<InvalidOperationException>.Now();
//Throw an exception based on a condition
Throw<InvalidOperationException>.When(obj == null);
Pitcher uses the MIT license, see the LICENSE file.