---
url: >-
  /statelake/scripting-reference/functions-and-procedures/exception-functions/exceptiontostring.md
---

# ExceptionToString

The function **ExceptionToString** returns an exception that has been formatted as a string error message.

Please also review [Try....Except](/statelake/scripting-reference/statements/try....except) for more detailed information.

**Declaration**: Function ExceptionToString(ExceptionType, ExceptionParam): string;

The following example shows how the plain text error message can be prefixed by an additional user-friendly explanation.

```pascal
procedure OnMapEvent(var Value:Variant);
var
  Int1, Int2, Int3 : 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.');
 //LogError will force the log to fail regardless
    LogError('Exception occurred: '+ExceptionToString(ExceptionType, ExceptionParam));

 end;
end;
```

To trap the exception and not cause the **Action** to fail and produce a **PINK** log, the **LogError** function can be replaced with either **LogWarning**, or a simple **LogInfo**, as illustrated below. The **Action** will continue and the Log will stay **GREEN**.

```pascal
procedure OnMapEvent(var Value:Variant);
var
  Int1, Int2, Int3 : 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
 //LogError will force the log to fail regardless, so use LogInfo (or LogWarning)
    LogInfo('Exception occurred: '+ExceptionToString(ExceptionType, ExceptionParam));
 end;
end;
```

Please also refer to [LogError](/statelake/scripting-reference/functions-and-procedures/logging-functions/logerror) and [LogWarning](/statelake/scripting-reference/functions-and-procedures/logging-functions/logwarning) for additional information on their use.
