Skip to content

MeltingMosaic/FluentExceptions

Repository files navigation

FluentExceptions

FluentExceptions allows you to write code that handles exceptions in a fluent manner, rather than using try/catch blocks.

Simple Example:

This snippet will eat an ArithmeticException

var exceptionThrower = new ExceptionThrower();

exceptionThrower.Invoking(e => e.MethodThatThrowsArithmeticException())
    .MayThrow<ArithmeticException>()
    .WhenExecuted();

You can also handle the exception:

var exceptionThrower = new ExceptionThrower();

exceptionThrower.Invoking(e => e.MethodThatThrowsArithmeticException())
    .MayThrow<ArithmeticException>()
    .HandledBy(e => Console.WriteLine(e.ToString()))
    .WhenExecuted();

Or you can catch multiple exceptions with one handler

var exceptionThrower = new ExceptionThrower();

exceptionThrower.Invoking(e => e.MethodThatThrowsSomeStuff())
    .MayThrow<ArithmeticException>()
    .Or<TimeoutException>()
    .Or<ArgumentNullException>()
    .HandledBy(e => Console.WriteLine(e.ToString()))
    .WhenExecuted();

Or define separate handling for different groups of exceptions:

var exceptionThrower = new ExceptionThrower();

exceptionThrower
    .Invoking(e => e.MethodThatThrowsSomeStuff())
        .MayThrow<ArithmeticException>()
        .Or<TimeoutException>()
        .HandledBy(e => Console.WriteLine(e.ToString()))
    .OrIt()
        .MayThrow<ArgumentNullException>()
        .HandledBy(e.=> Debug.WriteLine(e.ToString()))
    .WhenExecuted();

What about async/await?

Change .Invoking() to .Awaiting() and the handler now returns a Task (or Task<T>)

await exceptionThrower.Awaiting(o => o.AsyncVoidMethodThatThrowsArithmeticException())
    .MayThrow<ArithmeticException>()
    .HandledBy(e => Console.WriteLine(e))
    .WhenExecuted();

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages