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
  • ShouldNotThrowAction
  • ShouldNotThrow Action Extension
  • ShouldNotThrowFunc
  • ShouldNotThrow Func Extension
  • ShouldNotThrowFuncOfTask
Edit on GitHub
  1. Documentation
  2. Exceptions

NotThrow

PreviousThrowNextSatisfyAllConditions

Last updated 2 years ago

ShouldNotThrowAction

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 0;
Should.NotThrow(() =>
                {
                    var y = homer.Salary / denominator;
                });

|

Exception

`var y = homer.Salary / denominator;`
    should not throw but threw
System.DivideByZeroException
    with message
"Attempted to divide by zero."

ShouldNotThrow Action Extension

var homer = new Person { Name = "Homer", Salary = 30000 };
var denominator = 0;
var action = () =>
                {
                    var y = homer.Salary / denominator;
                };
action.ShouldNotThrow();

Exception

`action()`
    should not throw but threw
System.DivideByZeroException
    with message
"Attempted to divide by zero."

ShouldNotThrowFunc

string? name = null;
Should.NotThrow(() => new Person(name!));

Exception

`new Person(name!)`
    should not throw but threw
System.ArgumentNullException
    with message
"Value cannot be null. (Parameter 'name')"

ShouldNotThrow Func Extension

string? name = null;
var func = () => new Person(name!);
func.ShouldNotThrow();

Exception

`func()`
    should not throw but threw
System.ArgumentNullException
    with message
"Value cannot be null. (Parameter 'name')"

ShouldNotThrowFuncOfTask

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

Exception

`var task = Task.Factory.StartNew( () => { var y = homer.Salary / denominator; }); return task;`
    should not throw but threw
System.DivideByZeroException
    with message
"Attempted to divide by zero."

|

|

|

|

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