Skip to content

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 fluent syntax.

License

Notifications You must be signed in to change notification settings

pjmeca/Fluent.TryCatch

Repository files navigation

💧 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.

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 fluent syntax.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages