# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.shouldly.org/documentation/exceptions/throw.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
