AlinSpace.Exceptions
6.8.6
Prefix Reserved
dotnet add package AlinSpace.Exceptions --version 6.8.6
NuGet\Install-Package AlinSpace.Exceptions -Version 6.8.6
<PackageReference Include="AlinSpace.Exceptions" Version="6.8.6" />
paket add AlinSpace.Exceptions --version 6.8.6
#r "nuget: AlinSpace.Exceptions, 6.8.6"
// 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
A simple fluent library for exception handling.
Try.Catch(() => MightThrow(), e => { ... });
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 | 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. |
-
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.