# E4122

Invalid raise operation. Can only be used inside a function with error types in
its signature.

There are 3 raise operations in MoonBit, and they must be used inside a function
with error types in its signature or a try block.

- Use the `raise` keyword to raise an error directly.
- Re-raise an error by simply applying a function that raises an error.
- Explicitly handle the error using the `try ... catch! ...` block. The `catch!`
  means re-raise all the errors that are not handled in the catch block.

```{warning}
The `catch!` syntax is deprecated. Use `catch {e => raise e}` to reraise explicitly.
```

## Erroneous example

- Raise the error directly:

  ```{literalinclude} /sources/error_codes/4122_error/top_0.mbt
  :language: moonbit
  ```

- Re-raise the error:

  ```{literalinclude} /sources/error_codes/4122_error/top_1.mbt
  :language: moonbit
  ```

- Catch all errors using `catch!`:

  ```{literalinclude} /sources/error_codes/4122_error/top_2.mbt
  :language: moonbit
  ```

## Suggestion

You can either modify the surrounding function to have error types in its
signature:

```{literalinclude} /sources/error_codes/4122_fixed/top_0.mbt
:language: moonbit
```

Or use the `try ... catch ...` block to handle the error:

```{literalinclude} /sources/error_codes/4122_fixed/top_1.mbt
:language: moonbit
```
