For the complete documentation index, see llms.txt. This page is also available as Markdown.

Throw

The exception assertions pass when the thrown exception is the expected type or derives from it, like a catch clause. This applies whether the type is given as a type argument or as a Type. To require the exact type, call ShouldBeOfType on the returned exception.

ShouldThrowAction

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 1;
Should.Throw<DivideByZeroException>(() =>
                {
                    var y = homer.Salary / denominator;
                });

snippet source | anchor

Exception

`var y = homer.Salary / denominator;`
    should throw
System.DivideByZeroException
    but did not

ShouldThrowAsync

Task doSomething() => Task.CompletedTask;
var exception = await Should.ThrowAsync<DivideByZeroException>(() => doSomething());

snippet source | anchor

Exception

Task doSomething() should throw System.DivideByZeroException but did not

ShouldThrow Action Extension

snippet source | anchor

Exception

ShouldThrowFunc

snippet source | anchor

Exception

ShouldThrow Func Extension

snippet source | anchor

Exception

ShouldThrowFuncOfTask

snippet source | anchor

Exception

Last updated