> 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/throw.md).

# 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

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

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

**Exception**

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

## ShouldThrowAsync

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

[<sup>snippet source</sup>](https://github.com/shouldly/shouldly/tree/master/src/Shouldly.Tests/ShouldThrowAsync/FuncOfTaskScenarioAsync.cs#L91-L95) <sup>|</sup> [<sup>anchor</sup>](#snippet-ShouldThrowAsync)

**Exception**

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

## ShouldThrow Action Extension

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

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

**Exception**

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

## ShouldThrowFunc

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

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

**Exception**

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

## ShouldThrow Func Extension

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

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

**Exception**

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

## ShouldThrowFuncOfTask

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

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

**Exception**

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