VinlandSolutions.CommonCore.Exceptions
1.0.0-pre.7
dotnet add package VinlandSolutions.CommonCore.Exceptions --version 1.0.0-pre.7
NuGet\Install-Package VinlandSolutions.CommonCore.Exceptions -Version 1.0.0-pre.7
<PackageReference Include="VinlandSolutions.CommonCore.Exceptions" Version="1.0.0-pre.7" />
paket add VinlandSolutions.CommonCore.Exceptions --version 1.0.0-pre.7
#r "nuget: VinlandSolutions.CommonCore.Exceptions, 1.0.0-pre.7"
// Install VinlandSolutions.CommonCore.Exceptions as a Cake Addin
#addin nuget:?package=VinlandSolutions.CommonCore.Exceptions&version=1.0.0-pre.7&prerelease
// Install VinlandSolutions.CommonCore.Exceptions as a Cake Tool
#tool nuget:?package=VinlandSolutions.CommonCore.Exceptions&version=1.0.0-pre.7&prerelease
Vinland Solutions CommonCore.Exceptions Library
NOTE: This project is in alpha development and the public api is extremely unstable. Breaking changes are possible with each pre-release.
CommonCore.Exceptions is a .Net Standard 2.0/2.1 library designed to simplify the validating and throwing of common exceptions.
Installation
The official release versions of the CommonCore.Exceptions library are hosted on NuGet and can be installed using the standard console means, or found through the Visual Studio NuGet Package Managers.
This library is compatible with projects targeting at least .Net Framework 4.6.1, .Net Core 2.0, or .Net 5.
Usage
One simple use case is validating the extension argument of extension methods, throwing a NullReferenceException
if the argument is null
, imitating the behavior of non-extension methods. More generally, arguments can be validated for various states, throwing if the validation condition is met.
public static void SomeExtension(this object self, object model,
double angle, List<Point> points, int activePointIndex)
{
// Throws if 'self' is null, NullReferenceException.
Throw.If.SelfNull(self);
// Throws if model is null, ArgumentNullException.
Throw.If.Arg.Null(nameof(model), model);
// Throws if angle is lesser than 0, ArgumentOutOfRangeException.
Throw.If.Arg.Range.Less(nameof(angle), angle, 0.0);
// Throws if angle is greater than or equal to 360, ArgumentOutOfRangeException.
Throw.If.Arg.Range.MoreOrEqual(nameof(angle), angle, 360.0);
// Throws if points list is null, ArgumentNullException.
Throw.If.Arg.Null(nameof(points), points);
// Throws if points list is empty, ie Count == 0, ArgumentEmptyException.
Throw.If.Arg.Empty(nameof(points), points);
// Throws if activePointIndex is not a valid index into points list, ArgumentIndexOutOfRangeException.
Throw.If.Arg.Index.Invalid(nameof(activePointIndex), activePointIndex, points);
}
Occasionally greater control is needed and in these cases more specific library functionality can be used.
public static void SomeMethod(string text, int textIndex)
{
// Do a normal if-throw, but let Throw.As construct the exception.
if (text is null) { throw Throw.As.Arg.Null(nameof(text)); }
// Do a normal if-throw, but use Throw.Is validation and Throw.From message formatting,
// then throw your own custom exception.
if (Throw.Is.Range.Outside(textIndex, min: 2, max: 9, minInclusive: false, maxInclusive: true))
{
throw new MySpecialStringIndexException(nameof(textIndex),
Throw.From.Range.OutsideExclusiveInclusive(textIndex, 2, 9);
}
}
In certain situations, one may need to process an exception after it is constructed but before it is thrown. Such a situation can be handled using the alternative try-out-pattern.
public static void SomeMethod(string text, int textIndex)
{
List<Exception> exceptions = new();
{ if (Throw.Try.Arg.Null(nameof(text), text, out var exception) { exceptions.Add(excepion); } }
{ if (Throw.Try.Arg.Empty(nameof(text), text, out var exception) { exceptions.Add(excepion); } }
{ if (Throw.Try.Arg.Range.Less("text.Length", text.Length, 10, out var exception) { exceptions.Add(excepion); } }
{ if (Throw.Try.Arg.Index.Invalid(nameof(textIndex), textIndex, text, out var exception) { exceptions.Add(excepion); } }
if (exceptions.Count > 0)
{
throw new AggregateException(exceptions);
}
}
Projects
The CommonCore.Exceptions repository is composed of three projects with the listed dependencies:
- CommonCore.Exceptions: The dotnet standard 2.0/2.1 library project.
- CommonCore.Shims (dotnet standard 2.0 target)
- CommonCore.Exceptions.Docs: The project for generating api documentation.
- CommonCore.Exceptions.Tests: The project for running unit tests and generating coverage reports.
Links
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- VinlandSolutions.CommonCore.Shims (>= 1.0.0-pre.3)
-
.NETStandard 2.1
- VinlandSolutions.CommonCore.Shims (>= 1.0.0-pre.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on VinlandSolutions.CommonCore.Exceptions:
Package | Downloads |
---|---|
VinlandSolutions.CommonCore.Forms
CommonCore.Forms is a .Net 5 Windows Forms library ?. |
|
VinlandSolutions.CommonCore.Text
CommonCore.Text is a .Net Standard 2.0/2.1 library ?. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-pre.7 | 122 | 5/6/2022 |
1.0.0-pre.6 | 137 | 4/9/2022 |
1.0.0-pre.5 | 116 | 4/6/2022 |
1.0.0-pre.4.1 | 109 | 3/23/2022 |
1.0.0-pre.4 | 116 | 3/21/2022 |
1.0.0-pre.3 | 114 | 3/19/2022 |
1.0.0-pre.2 | 113 | 3/17/2022 |
1.0.0-pre.1 | 179 | 5/9/2021 |
# Release v1.0.0-pre.7
* Removed all deprecated code #23
* Add `Path` hooks #29
## Dependencies
* [CommonCore.Shims](https://gitlab.com/commoncorelibs/commoncore-shims) made a non-conditional dependency #28