The On Error statement sets the behaviour for the current running procedure how to cope with errors. There are two flavours:
On Error Resume Next | Set the behaviour to ignore any errors. This will resume operation to the next statement, even if an error occured. The Err object can be used to see if something went wrong. |
On Error Goto 0 | This is the default behaviour. Will break on errors. If some error-handling is specified in a calling routine 'above' then the current routine will stop executing and the error will bubble-up. |
Function OnErrorDemo
Dim lErrorMessage As String
On Error Resume Next
DoSomeDatabaseUpdate 'Here, an error may or may not happen
DoSomeOtherDatabaseUpdate 'Here, an error may or may not happen
'Note: Each statement above will be executed
If Err.Number <> 0 Then
lErrorMessage = Err.Description 'This will contain the last Error message.
End If
Return lErrorMessage
End Function
Please notice the following:
The Try-Catch statement is new in Polar Script. It will try to execute a number of lines. Once an error occurs, the rest of the lines is NOT executed (that is different from the On-Error-Resume-Next) and the execution will proceed to the Catch block or jump to the End Try. Using a Catch block is optional; within the Catch block, no error-handling is active anymore.
Note: In Polar Script, the 'Catch' block does not take any parameters. This is different from some other languages where the 'Catch' block can capture an exception object. In Polar Script, to access error details after catching an error, you should use the 'Err' object, as demonstrated in the Try-Catch examples.
Function TryCatchDemo
Dim lErrorMessage As String
Try
DoSomeDatabaseUpdate 'Here, an error may or may not happen
DoSomeOtherDatabaseUpdate 'Here, an error may or may not happen
'Note: The second statement above will only be executed if the first succeeded
Catch
lErrorMessage = Err.Description
End Try
Return lErrorMessage
End Function