---
name: "try"
type: command
status: ok
groups: ["Program Flow"]
arma3_since: "0.50"
locality: {"argument": null, "effect": null, "server_exec": false}
source: https://community.bistudio.com/wiki/try
revid: 369368
updated: 2024-09-03T18:43:09Z
---

# try

*Command · Program Flow · Arma 3 v0.50 · introduced in Armed Assault v1.00*

## Description

Defines a try-catch structure. This sets up an exception handling block. Any thrown exception in a try block is caught in a [catch](../commands/catch.md) block. The structured exception block has the following form:

```sqf
try
{ /* block that can throw exception */ }
catch
{ /* block that processes the exception. Exception is described in the _exception variable */ };
```

## Syntax

### Syntax 1

```sqf
try code
```

**Parameters:**

- `code`: [Code](../topics/Code.md)

**Returns:** Exception Type

### Syntax 2

*since arma3 1.54*

```sqf
args try code
```

**Parameters:**

- `args`: [Anything](../topics/Anything.md) - passed arguments, will be put in *_this* variable inside the "code"
- `code`: [Code](../topics/Code.md)

**Returns:** Exception Type

## Examples

### Example 1

```sqf
try { throw "invalid argument" } catch { hint str _exception };
```

### Example 2

```sqf
123 try { if (_this != 123) throw "invalid argument" } catch { hint str _exception };
```

### Example 3

```sqf
try {
    if (a > b) throw "Error: some error"; // OK
    // the command argument is static
} catch {
    hint str _exception;
};

try {
    _someFunc = {
        // ...
    };
    if (a > b) throw (call _someFunc); // NOT OK
    // the command argument is dynamic
    // _someFunc is called first to get the value regardless of (a > b) outcome
} catch {
    hint str _exception;
};

try {
    _someFunc = {
        // ...
    };
    if (a > b) then {throw (call _someFunc)}; // OK
    // the command argument is dynamic
    // _someFunc is only called when (a > b) is true
} catch {
    hint str _exception;
};
```

## See also

Exception handling · [throw](../commands/throw.md) · [catch](../commands/catch.md)

## Community notes

**Killzone_Kid** — 2015-06-09

Do not expect this behave like Javascript try catch and ignore all errors. But it does have one useful behaviour. Normally when a runtime error occurs in SQF (unlike when there is compile error) it continues to execute till the end. But if the script is placed in [try](../commands/try.md) {} scope and [throw](../commands/throw.md) is used upon error, the script immediately terminates, exits the [try](../commands/try.md) {} scope and enters [catch](../commands/catch.md) {} scope. This way it is possible to process possible exceptions in civilised manner.

<sub>Source: [BI Community Wiki — try](https://community.bistudio.com/wiki/try) (revision 369368).</sub>
