Shouldly
  • Overview
  • Contributing
  • Documentation
    • Getting Started
    • Configuration
    • Equality
      • ShouldBe
      • NotBe
      • Null
      • Bool
      • Flags
      • AssignableTo
      • OfType
      • OneOf
      • Greater/Less Than
      • InRange
      • MatchApproved
      • Enumerable
      • SameAs
      • String
      • ExampleClasses
    • String
      • ShouldBe
      • Match
      • Contain
      • Null and Empty
      • StartWith
      • EndWith
    • Enumerable
      • ShouldBe
      • All
      • Empty
      • OneOf
      • Contain
      • Unique
      • SubsetOf
      • Have
    • Dictionary
      • ContainKey
      • ContainKeyAndValue
    • Exceptions
      • Throw
      • NotThrow
    • SatisfyAllConditions
    • CompleteIn
    • DynamicShould
    • Upgrade 3 to 4
Powered by GitBook
On this page
  • ShouldThrowAction
  • ShouldThrowAsync
  • ShouldThrow Action Extension
  • ShouldThrowFunc
  • ShouldThrow Func Extension
  • ShouldThrowFuncOfTask
Edit on GitHub
  1. Documentation
  2. Exceptions

Throw

PreviousExceptionsNextNotThrow

Last updated 10 months ago

ShouldThrowAction

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

|

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());

|

Exception

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

ShouldThrow Action Extension

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

Exception

`action()`
    should throw
System.DivideByZeroException
    but did not

ShouldThrowFunc

Should.Throw<ArgumentNullException>(() => new Person("Homer"));

Exception

`new Person("Homer")`
    should throw
System.ArgumentNullException
    but did not

ShouldThrow Func Extension

var func = () => new Person("Homer");
func.ShouldThrow<ArgumentNullException>();

Exception

`func()`
    should throw
System.ArgumentNullException
    but did not

ShouldThrowFuncOfTask

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

Exception

Task `var task = Task.Factory.StartNew( () => { var y = homer.Salary / denominator; }); return task;`
    should throw
System.DivideByZeroException
    but did not

|

|

|

|

snippet source
snippet source
anchor
anchor
snippet source
anchor
snippet source
anchor
snippet source
anchor
snippet source
anchor