> For the complete documentation index, see [llms.txt](https://docs.shouldly.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.shouldly.org/documentation/exceptions/notthrow.md).

# NotThrow

## ShouldNotThrowAction

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowAction.codeSample.approved.cs#L1-L6) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowAction.codeSample.approved.cs)

**Exception**

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

## ShouldNotThrow Action Extension

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowActionExtension.codeSample.approved.cs#L1-L7) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowActionExtension.codeSample.approved.cs)

**Exception**

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

## ShouldNotThrow specific exception

`ShouldNotThrow<TException>` verifies that a specific exception type is not thrown. Exceptions of other types are not caught and will propagate to the caller.

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowSpecificException.codeSample.approved.cs#L1-L6) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowSpecificException.codeSample.approved.cs)

**Exception**

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

## ShouldNotThrowFunc

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFunc.codeSample.approved.cs#L1-L2) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowFunc.codeSample.approved.cs)

**Exception**

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

## ShouldNotThrow Func Extension

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFuncExtension.codeSample.approved.cs#L1-L3) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowFuncExtension.codeSample.approved.cs)

**Exception**

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

## ShouldNotThrowFuncOfTask

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/DocumentationExamples/CodeExamples/ShouldNotThrowExamples.ShouldNotThrowFuncOfTask.codeSample.approved.cs#L1-L11) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldNotThrowExamples.ShouldNotThrowFuncOfTask.codeSample.approved.cs)

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