Fluent.TryCatch
1.1.0
dotnet add package Fluent.TryCatch --version 1.1.0
NuGet\Install-Package Fluent.TryCatch -Version 1.1.0
<PackageReference Include="Fluent.TryCatch" Version="1.1.0" />
<PackageVersion Include="Fluent.TryCatch" Version="1.1.0" />
<PackageReference Include="Fluent.TryCatch" />
paket add Fluent.TryCatch --version 1.1.0
#r "nuget: Fluent.TryCatch, 1.1.0"
#:package Fluent.TryCatch@1.1.0
#addin nuget:?package=Fluent.TryCatch&version=1.1.0
#tool nuget:?package=Fluent.TryCatch&version=1.1.0
💧 Fluent.TryCatch
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
tryclause - Zero to multiple
catchblocks - Optionally, a
whenclause for eachcatchblock - A special
ThrowAsoperator to change the type of exception thrown. - A special
ignoreoperator that discards non controlled exceptions - Zero to one
finallyblock - 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 | Versions 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. |
-
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.
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.