<!-- https://learnsql.cpetoday.com/functions/m/error -->
# error

Language: Power Query M
Category: Language
Syntax: error errorRecord
Summary: Raises an error on purpose, using an error record, so that the query stops with a clear message.
Returns: Nothing. It stops the expression it is in and reports the error.
Level: Intermediate
Related: Error.Record, try, otherwise, if, Value.Is

## What it does
The `error` keyword raises an error. You use it inside an `if` or another expression when something must not continue, such as a control total that does not agree or a file that should not be empty. The error carries a reason and a message, which you build with Error.Record. Without `error`, a bad input can pass silently.

## Syntax and arguments
- `errorRecord`: an error record, normally made with Error.Record(reason, message, detail).

## Example
A month-end file must balance: the total of the debit lines must equal the total of the credit lines. If it does not, the refresh stops and the reviewer is told by how much.

```powerquery
let
    Debits = {1250, 480, 75},
    Credits = {1805, 0},
    DebitTotal = List.Sum(Debits),
    CreditTotal = List.Sum(Credits),
    Balanced = if DebitTotal = CreditTotal
        then "Balanced"
        else error Error.Record("Unbalanced file",
            "Debits and credits do not agree.",
            DebitTotal - CreditTotal)
in
    Balanced
```

Here the totals agree, so `Balanced` is returned. If a credit line were missing, the query would stop with the reason "Unbalanced file" and the difference in the detail, which the reviewer can read at once.

## Example: a second, different use
Stopping a load when a required rate is missing from the rate table, so that no report is produced with a silent gap.

## Things to watch
- An error stops the step it is in. Place the check where you want the refresh to stop.
- A `try` around the check catches the error, so do not hide a control with a try unless you handle it on purpose.
- Keep the reason short and the same for each check, so people can search for it.
- The error is not a warning. Use it for failures that must be fixed before the report is used.

## In business terms
Control checks that must pass before a report is used, such as a balanced file, a complete period or a matching control total, are written with `error`. A failed check that stops the refresh is stronger evidence than a note in a spreadsheet.
