AlinSpace.Exceptions 6.8.6

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
.NET 6.0
dotnet add package AlinSpace.Exceptions --version 6.8.6
NuGet\Install-Package AlinSpace.Exceptions -Version 6.8.6
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="AlinSpace.Exceptions" Version="6.8.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AlinSpace.Exceptions --version 6.8.6
#r "nuget: AlinSpace.Exceptions, 6.8.6"
#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.
// Install AlinSpace.Exceptions as a Cake Addin
#addin nuget:?package=AlinSpace.Exceptions&version=6.8.6

// Install AlinSpace.Exceptions as a Cake Tool
#tool nuget:?package=AlinSpace.Exceptions&version=6.8.6

AlinSpace.Exceptions

NuGet version (AlinSpace.Exceptions)

A simple fluent library for exception handling.

Try.Catch(() => MightThrow(), e => { ... });

NuGet package

Why?

Sometimes exception handling (try-catch-finally) can take up a lot of lines of code. In many situations this can make the code less readable and maintainable. This library tries to improve it by employing a more fluent and functional exception handling design. Not always is the fluent and functional approach of handling exceptions the best, it always depends.

Examples - All exception types

Here are some examples where the fluent approach is much better.

// Bad (needs 8 lines)
try
{
   // Often there is only a single line in the try-block.
   DoSomethingThatMightThrow();
}
catch
{
   // ignore
}
 
// Bad (needs 8 lines)
try
{
   DoSomethingThatMightThrow();
}
catch(Exception e)
{
   // Often there is only a single line in the catch-block.
   logger.Error(e);
}

// Bad (needs 8 lines)
try
{
   DoSomethingThatMightFail();
}
finally
{
   DiposeResource();
}
 
// Good (only 1 line)
Try.Catch(DoSomethingThatMightThrow, e => logger.Error(e));

// Good (only 1 line)
Try.CatchIgnore(DoSomethingThatMightThrow);

// Good (only 1 line)
Try.Finally(DoSomethingThatMightFail, DisposeResource);

The following code snippet would take up a lot more lines of code when writing it in a traditional way:

public void Dispose()
{
   Try.CatchIgnore(DisposeResourceA);
   Try.CatchIgnore(DisposeResourceB);
   Try.CatchIgnore(DisposeResourceC);
   Try.CatchIgnore(DisposeResourceD);
}

Examples - Specific exception types

The above examples always acts upon all types of thrown exceptions. You can also specify the exact type of exception that you are interested in. All other exception types will simply propagate.

// Will catch the exception.
Try.Catch<MyException>(() => throw new MyException(), e => logger.Log(e));

// Will not catch the exception.
Try.Catch<MyException>(() => throw new ArgumentNullException(), e => logger.Log(e));

Examples - Asynchronous code

Everything also works fully with asynchronous code:

await Try.CatchAsync(MaybeThrowsAsync, e => logger.Error(e));

Examples - Return values

It is also possible to let the catch delegate return a value. If an exception is thrown, it will be handled accordingly and a default value will be returned instead.

// Returns the number on success or null on any exceptions.
var number = await Try.CatchReturnAsync<int?>(() => GetNumberOrThrow(), e => logger.Error(e));

// Returns the number on success or 5 on any exceptions.
var number = await Try.CatchReturnAsync(() => GetNumberOrThrow(), e => logger.Error(e), defaultValue: 5);
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on AlinSpace.Exceptions:

Package Downloads
AlinSpace.Linux The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

AlinSpace.Docker The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.8.6 631 11/8/2022
6.8.5 262 11/1/2022
6.8.4 258 11/1/2022
6.8.1 302 10/12/2022
6.8.0 300 10/12/2022
6.1.2 298 10/12/2022
6.1.0 337 5/26/2022
6.0.2 362 2/9/2022
6.0.0 1,278 11/20/2021
5.0.1 307 7/31/2021
5.0.0 258 7/30/2021
2.1.0 256 7/30/2021
2.0.0 247 7/30/2021