---
title: "lint/pest/prefer-to-throw"
pagination: false
---

{/* Generated by scripts/gen-rule-docs.py. Edit mapping.toml, not this file. */}

<RuleId id="lint/pest/prefer-to-throw" />

In the [`coding-style`](/docs/rules/sets/coding-style) set. Safe fix. Runs when pest is installed.

## What it does

Use expect()->toThrow() instead of expectException() or try/catch in Pest tests.

## Configuration

```toml title="pun.toml"
[lint.rules]
"pest/prefer-to-throw" = "error"
```

To skip it in some files or lines, add an [override](/docs/configuration/pun-toml#lint) or an [ignore comment](/docs/configuration/ignore-comments).

## Options

| Name | Type | Allowed values | Default | Upstream name |
| --- | --- | --- | --- | --- |
| `checks` | `list` | `expect-exception`, `try-catch` | `["expect-exception", "try-catch"]` | None (Pun's own) |

## Replaces

- [`ConvertExpectExceptionToThrow`](https://pestphp.com/docs/rector#content-convertexpectexceptiontothrowrector) (pest-plugin-rector)
- [`UseToThrow`](https://pestphp.com/docs/rector#content-usetothrowrector) (pest-plugin-rector)

## Merged rules

These old IDs are now this rule. A config that still names one stops with an error. See [merged rules](/docs/migrate/merged-rules).

- `lint/pest/no-expect-exception`

[More lint/pest rules](/docs/rules/lint/pest) · [Edit this page](https://github.com/shipfastlabs/pun/edit/main/crates/pun-config/rules/mapping.toml)
