Fluent.TryCatch 1.1.0

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

💧 Fluent.TryCatch

GitHub Repo stars NuGet Downloads GitHub License

Table of Contents

About

Fluent.TryCatch is a lightweight library that adds support for fluent try...catch statements to .Net. It incorporates all the funtionality of the standard try...catch statement, but with a more fluent syntax.

Why?

Simplicity. Sometimes the standard try...catch statement can be verbose and difficult to read, especially for small segments of code. The fluent syntax offered by this library makes it easier to write and understand.

Features

  • One try clause
  • Zero to multiple catch blocks
  • Optionally, a when clause for each catch block
  • A special ThrowAs operator to change the type of exception thrown.
  • A special ignore operator that discards non controlled exceptions
  • Zero to one finally block
  • Returning values

Examples

A simple try...catch statement

Fluent.Try(() =>
        // Your code here
    ).Catch(e =>
        // Handle Exception
    ).Execute();

Translates to:

try
{
    // Your code here
}
catch (Exception e)
{
    // Handle Exception
}

Multiple try...catch statements with a when clause

Fluent.Try(() => 
        // Your code here
    ).Catch<ArgumentOutOfRangeException>(e => 
        // Handle ArgumentOutOfRangeException
    ).Catch(e => 
        // Handle OutOfMemoryException or ArgumentNullException
    ).When(e => e is OutOfMemoryException || e is ArgumentNullException)
    .Catch(e => 
        // Handle Exception
    )
    .Execute();

Translates to:

try
{
    // Your code here
}
catch (ArgumentOutOfRangeException e)
{
    // Handle ArgumentOutOfRangeException
}
catch (Exception e) when (e is OutOfMemoryException || e is ArgumentNullException)
{
    // Handle OutOfMemoryException or ArgumentNullException
}
catch (Exception e)
{
    // Handle Exception
}

The ignore operator with a finally block

Fluent.Try(() => 
        // Your code here
    ).Ignore()
    .Finally(() =>
        // Your code here
    )
    .Execute();

Translates to:

try
{
    // Your code here
}
catch (Exception e)
{
    // Empty catch
}
finally
{
    // Your code here
}

Returning values

int result = Fluent.Try(() => 
        return 1;
    ).Catch<ArgumentNullException>(e =>
        return 2;
    ).Catch(e =>
        // No return value -> default value is returned
    ).Execute<int>();

Translates to:

int result;
try
{
    result = 1;
}
catch (ArgumentNullException e)
{
    result = 2;
}
catch (Exception e)
{
    result = default; // 0
}

Note: If a block does not return a value, the default value will be automatically returned.

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.  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.
  • 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
1.1.0 3,354 3/31/2024
1.0.1 253 1/28/2024

v.1.1.0
- Added ThrowAs operator
- Display documentation in release
v.1.0.1
- Change namespace to Fluents to avoid name conflicts
v.1.0.0
- Initial release.