Try...Except creates a block of statements, and provides for error handling. Any other statement that allows entry of a single statement can be used with Try...Except to allow entry of multiple statements.
Try...Except has an exception block will allows you to execute specific code only if an error occurs. This error handling allows for you to execute other code if an exception occurs within the code you write. You could therefore provide custom error handling or run alternatively code to set the field to another value. The following is an example of Try...Except
procedure ScriptEvent (var Value : variant);
begin
if Value>10 then
try
LogInfo('this is the first statement to execute if Value is greater than 10');
LogInfo('this is the second statement to execute if Value is greater than 10');
except
LogError('Exception: '+ExceptionToString(ExceptionType, ExceptionParam));
//you could call RaiseException here to have Flow continue to handle the error
//Otherwise Flow does not see this error and will not fail the mapping
end;
end;
In the above example the LogInfo code lines will execute if Value is greater than 10. If however there is an error executing either of the LogInfo lines then an exception will be caused and the LogError code line will be executed.
You only need to trap exceptions using Try...Except block if you want to provide custom handling for the error. If a Try...Except block is not used the error will be handled automatically by Flow and will appear in the Log.
You should be careful when handling the exception yourself as you will need to notify Flow whether you want to fail the map (LogError is one such way to notify that an error has occurred and therefore fail the map). To notify Flow of the error you can also call See "RaiseException" to raise the error outside your Try...Except block so it will also be handled automatically by Flow.
The following example provides an alternative value to the field in case of errors. This can be useful instead of failing the mapping.
procedure ScriptEvent (var Value : variant);
begin
try
Value := 1 /0; //this will cause a divide by 0 error
except
LogWarning('Exception: '+ExceptionToString(ExceptionType, ExceptionParam));
//we could now default this field to another value
Value := 0;
end;
end;
Try...Except blocks can be nested.
Each of the other statements in this section include examples of using them with and without Try...Except blocks.
See "Begin...End" is another form for creating blocks of statements the only difference being it does not support error handling.