> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Control - try...catch

CRMScript has a set of special statements to handle error-situations in your own scripts and react accordingly. This gives you the opportunity to recover, or fail gracefully instead of crashing.

## Try statement

The `try` statement contains the code you want to run but acknowledge might fail at some point.

The definition has 2 parts:

* keyword **try**
* 1 or more statements, enclosed in curly brackets - \{} must always be used

```crmscript theme={null}
try {
  myFunction();
}
```

## Catch statement

The `catch` statement contains the code to run if an exception is thrown from the `try` block.

The definition has 2 parts:

* keyword **catch**
* 1 or more statements, enclosed in curly brackets - \{} must always be used

```crmscript theme={null}
catch {
  printLine(error);
}
```

## Built-in exceptions

The built-in classes and functions of CRMScript may throw exceptions, which you can catch.
Inside the `catch` block, you can use the following implicit String variables:

* **error** - a status code or description of the error
* **errorLocation** - the location where the exception was thrown

```crmscript! theme={null}
try {
  Integer i = 123;
  Integer j = 0;
  Integer k = i/j;
}
catch {
  printLine("Exception caught: " + error);
  printLine("...at " + errorLocation);
}
```

## Custom exceptions

You can also throw you own exceptions from the `try` block.

The `throw` statement has 2 parts:

* keyword **throw**
* a string (in quotes) followed by a semicolon

Preferably place the `throw` statement inside a `try` block. If you throw an exception outside `try`, it will eventually be caught by us and shown as if the script failed.

```crmscript! theme={null}
try {
  throw "This is my own error";
}
catch {
  printLine(error);
}
```

This will print **GeneralException:** followed by the text string you threw.


## Related topics

- [Fundamentals of CRMScript](/en/automation/crmscript/fundamentals/index.md)
- [Insert, update, and delete](/en/automation/crmscript/searchengine/se-insert-update-delete.md)
- [Reserved words](/en/automation/crmscript/fundamentals/reserved.md)
- [Erp Sync engine operation](/en/api/plugins/erp-connectors/sync-operation.md)
- [Try Connect Actor](/en/api/reference/restful/agent/erpsync_agent/try-connect-actor.md)
- [Try Add Persons To Email Flow](/en/api/reference/restful/agent/workflow_agent/try-add-persons-to-email-flow.md)
