ExceptionHelpers 1.3.0

dotnet add package ExceptionHelpers --version 1.3.0
                    
NuGet\Install-Package ExceptionHelpers -Version 1.3.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ExceptionHelpers" Version="1.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ExceptionHelpers" Version="1.3.0" />
                    
Directory.Packages.props
<PackageReference Include="ExceptionHelpers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ExceptionHelpers --version 1.3.0
                    
#r "nuget: ExceptionHelpers, 1.3.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ExceptionHelpers@1.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ExceptionHelpers&version=1.3.0
                    
Install as a Cake Addin
#tool nuget:?package=ExceptionHelpers&version=1.3.0
                    
Install as a Cake Tool

ExceptionHelpers

Collection of Extension methods to easily validate parameters

How to use

All checks are implemented as extension methods. That means you can invoke them directly on the parameter in question:

public void Method(object parameter)
{
    parameter.ThrowIfNull();
    ...
}

All checks return the parameter they were called on (unless they throw an exception of course). This allows you to chain multiple checks or use them in assignments:

public void Constructor(String strParameter, object objParameter)
{
    this.ImportantObject = objParameter.Throw()
    this.importantString = strParameter.ThrowIfNullOrWhiteSpace()
                                       .ThrowIfInvalidLength(3,15);
}

All checks have default values for the Exceptions that are thrown as well as the generated messages. These can be overriden by providing additional Parameters to the check-Method:

public void Method()
{
    string str = null;
    str.ThrowIfNull("my message"); 
    //Result is a ArgumentNullException with the included message

    Exception ExceptionProvider(string? parameterName, string message) 
        => new CustomException(message, parameterName);
    str.ThrowIfNull("msg",  ExceptionProvider); 
    // Result is a exception of CustomException 
    // (possibly including the message, depending on the implementation of the constructor)
}

Possible extension

Checking the source code in this repo it should not be difficult to include support for more types. If you think this very basic set is missing some important types, feel free to create an issue, and maybe I will get around to it.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.3.0 3,311 9/7/2025
1.2.0 609 3/27/2025