# Throw

## ShouldThrowAction

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

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowAction.codeSample.approved.cs#L1-L6) | [anchor](#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());
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/Shouldly.Tests/ShouldThrowAsync/FuncOfTaskScenarioAsync.cs#L91-L95) | [anchor](#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>();
```

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

**Exception**

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

## ShouldThrowFunc

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

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowFunc.codeSample.approved.cs#L1-L1) | [anchor](#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>();
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowFuncExtension.codeSample.approved.cs#L1-L2) | [anchor](#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;
                });
```

[snippet source](https://github.com/shouldly/shouldly/blob/master/src/DocumentationExamples/CodeExamples/ShouldThrowExamples.ShouldThrowFuncOfTask.codeSample.approved.cs#L1-L11) | [anchor](#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
```
