D20Tek.Exceptions.ThrowIf 0.9.1

dotnet add package D20Tek.Exceptions.ThrowIf --version 0.9.1
                    
NuGet\Install-Package D20Tek.Exceptions.ThrowIf -Version 0.9.1
                    
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="D20Tek.Exceptions.ThrowIf" Version="0.9.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="D20Tek.Exceptions.ThrowIf" Version="0.9.1" />
                    
Directory.Packages.props
<PackageReference Include="D20Tek.Exceptions.ThrowIf" />
                    
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 D20Tek.Exceptions.ThrowIf --version 0.9.1
                    
#r "nuget: D20Tek.Exceptions.ThrowIf, 0.9.1"
                    
#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.
#addin nuget:?package=D20Tek.Exceptions.ThrowIf&version=0.9.1
                    
Install D20Tek.Exceptions.ThrowIf as a Cake Addin
#tool nuget:?package=D20Tek.Exceptions.ThrowIf&version=0.9.1
                    
Install D20Tek.Exceptions.ThrowIf as a Cake Tool

exceptions-throwif

Introduction

Welcome to D20Tek.Exceptions.ThrowIf. This library contains extensions to the ArgumentException.ThrowIf methods to cover more scenarios and additional exception types. It can be used in any .NET project, whether it is a console application, WebApi, or Blazor application.

Currently we have extension class that derive from Exceptions and then add static methods to create the base exception type. In .NET 10, we will update this library to use the new Extension Members feature to add static methods to the base exception type and make the coding experience even more seamless.

In the future, we may support more exception types and extensions to make Exception throwing easier. If you have suggestions for Exception extensions, please leave feedback in this repo's Issues page.

Installation

This library is a NuGet package so it is easy to add to your project. To install the package into your solution, you can use the NuGet Package Manager. In PM, please use the following command:

PM > Install-Package D20Tek.Exceptions.ThrowIf -Version 1.0.0

To install in the Visual Studio UI, go to the Tools menu > "Manage NuGet Packages". Then search for D20Tek.Exceptions.ThrowIf, and install whichever packages you require from there.

Usage

Once you've installed the NuGet package, you can start using it in your .NET projects. It's as easy has using the D20Tek.Exceptions.ThrowIf namespace and calling the exception extension.

Here is an example of using it to check whether an array is null or empty:

    var list = new int[] { 3, 7, 5, 9 };

    ArgumentNullExceptionExt.ThrowIfNullOrEmpty(list);

Or throwing an ArgumentOutOfRangeException that checks a value between a min and max range:

    var check = 3.14f;

    ArgumentOutOfRangeExceptionExt.ThrowIfOutOfRange(check, 1.0f, 10.0f);

There is also a helper ThrowIf extension that lets you throw any exception type based on a condition. This extension is useful for covering other exceptions that we have not added yet, or for using the ThrowIf syntax with your own custom Exception class.

    public class MyCustomException : Exception
    {
        public MyCustomException(string message)
            :base(message) { }
    }

    int v = 12;
    ExceptionExt.ThrowIf<MyCustomException>(() => v < 0, "Invalid value, use custom exception");

    ExceptionExt.ThrowIf<MyCustomException>(() => v >= 10, "Invalid value, use custom exception - this one throws");

Note: the custom exception class must derive from Exception, and it must have at minimum one constructor that takes a message as a string.

Samples

Samples will be coming soon...

Feedback

If you use this library and have any feedback, bugs, or suggestions, please file them in the Issues section of this repository. I'm still in the process of building the library and samples, so any suggestions that would make it more useable are welcome.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.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
0.9.1 140 5/20/2025