AlinSpace.Exceptions 6.8.6

The ID prefix of this package has been reserved for one of the owners of this package by Prefix Reserved
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


NuGet version (AlinSpace.Exceptions)

A simple fluent library for exception handling.

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

NuGet package


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)
   // Often there is only a single line in the try-block.
   // ignore
// Bad (needs 8 lines)
catch(Exception e)
   // Often there is only a single line in the catch-block.

// Bad (needs 8 lines)
// Good (only 1 line)
Try.Catch(DoSomethingThatMightThrow, e => logger.Error(e));

// Good (only 1 line)

// 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()

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.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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
6.8.6 1,376 11/8/2022
6.8.5 734 11/1/2022
6.8.4 723 11/1/2022
6.8.1 803 10/12/2022
6.8.0 829 10/12/2022
6.1.2 790 10/12/2022
6.1.0 857 5/26/2022
6.0.2 870 2/9/2022
6.0.0 1,769 11/20/2021
5.0.1 848 7/31/2021
5.0.0 760 7/30/2021
2.1.0 766 7/30/2021
2.0.0 728 7/30/2021