NotThrow

ShouldNotThrowAction

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

snippet source | anchor

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

snippet source | anchor

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

snippet source | anchor

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

snippet source | anchor

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

snippet source | anchor

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."

Last updated