---
url: /statelake/scripting-reference/statements/try....except.md
---

# Try....Except

Just like the **Begin….End** statement, **Try...Except** uses the keywords **Try** and **Except** to create an enclosure that contains a block of statements. However, the major difference between these statements, is that **Try…Except** provides for error handling.

Using **Try….Except**, allows the statements defined within the keyword enclosure to be viewed and treated as if they were a single statement, allowing the statements to become the body of a function or procedure and all executed together.

**Try...Except** has an exception block that allows you to execute specific code, only if an error occurs, allowing you to execute other code if an exception occurs within the code you write.

You can therefore easily provide custom error handling or run alternative code to set other values or set a correct sample value.

There is no limit to the number of statements that can be contained within the **Try….Except** enclosure block.

Comments can be included within the **Try...Except** statement block.

The following is an example of a **Try...Except** statement that also contains comment lines that will not be executed.

```pascal
procedure ScriptEvent (var Value : variant);
var Int1, Int2, Int3, Int4, Int5 : integer ;
begin
 try
 //Force this into error state by creating a divide by zero error
    Int1 := 10;
    Int2 := 0;
    Int3 := Int1 / Int2;

 //These lines will execute if no error
    LogInfo('This is the 1st statement to execute if there is no error.');
    LogInfo('This is the next statement to execute if there is no error.');
 except
    LogInfo('Error occurred.');

 //LogError will force the action to immediately fail, resulting in a pink log
    LogError('Exception: '+ExceptionToString(ExceptionType, ExceptionParam));

 //You could call RaiseException here to have Statelake continue to handle the error

 end;

  LogInfo('The script keeps processing.');
  LogInfo('But because of the LogError it will fail regardless. . . ');
  Int4 := 12;
  Int5 := 10;
  LogInfo('Calculated value is - '+IntToStr(Int4*Int5));
  LogInfo('Final line. . . ');

end;
```

In the above example, the **LogInfo** code lines will execute if the Value is greater than 10.

If however, there is an error when executing either of the **LogInfo** lines, then an exception will be caused and the **LogError** code line following the **except** will be executed.

You only need to trap exceptions using the **Try...Except** statement block if you want to provide custom handling for the error. If a **Try...Except** block is not used, then the error will be handled automatically by **Statelake** and will appear in the **Log**.

::: warning
You should be careful when handling any exception yourself, as you will need to notify **Statelake** whether you want to fail the **Map**. Using **LogError** is one such way to notify that an error has occurred. To notify **Statelake** of the error, you can also call **RaiseException** to raise the error outside of your **Try…Except** block, so that it will also be handled automatically by **Statelake**.
:::

The following example provides an alternative value to the field in case of errors. This can be useful instead of failing the mapping. Note that the comments prefixed by the // (double dash) will be ignored.

```pascal
procedure ScriptEvent (var Value : variant);
var
  Int1, Int2, Int3, Int4, Int5 : integer;
begin
 //Try to divide an integer by zero - to raise an exception
 try
    Int1 := 10;
    Int2 := 0;
    Int3 := Int1 / Int2;

 //These lines will execute if no error
    LogInfo('This is the 1st statement to execute if there is no error.');
    LogInfo('This is the next statement to execute if there is no error.');
 except
    LogInfo('Error occurred.');

 //LogWarning will message the Log of an issue
    LogWarning('Exception: '+ExceptionToString(ExceptionType, ExceptionParam));

 //But we could now default this field to another value
    Int3 := 0;
 end;

  LogInfo('The script keeps processing.');
  LogInfo('Regardless of the error, because of the LogWarning . . . ');
  Int4 := 12;
  Int5 := 10;
  LogInfo('Calculated value is - '+IntToStr(Int4*Int5));
  LogInfo('Final line. . . ');

end;
```

And just like **Begin….End**, **Try...Except** blocks can be nested.

**Begin….End** is another form for creating blocks of statements but this statement block does not support error handling. Please see [Begin....End](/statelake/scripting-reference/statements/begin....end) for further details.
