FluentExceptions allows you to write code that handles exceptions in a fluent manner, rather than using try/catch blocks.
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();