LMD ScriptPack Guide
NativeVB provides true exceptions support through Try/Catch/Finally and Throw statements. The Throw statement can be used to throw exceptions, like this:
If X > 10 Then
Throw New Exception("X is too big")
Note, that here New Exception(...) is just an expression that creates an instance of the exception. So, to really use the Throw statement, it is required to add imported VCL unit(s) to the script-control to allow script-code to use exception types (Exception in this case). Otherwise, you will get "Undeclared identifier" run-time error.
Your program can use Try/Catch/Finally statement to catch raised exceptions and execute error handling code. The Try statement can contain one or more conditional Catch blocks, single unconditional Catch block and a single Finally block. Unconditional Catch block should reside after all conditional catches, and the Finally block should be the last block in a Try statement. All of these blocks are optional and can be omit; however, at least one block is required. Conditional Catch blocks will catch exceptions only of the specified class (or subclass of); unconditional Catch block will catch all exceptions. Note, that even empty unconditional Catch block catches all possible exceptions, however, if the unconditional Catch block is omit, then all not caught exceptions will be thrown away from the current Try statement.
Try
DoSomething
Catch
MsgBox("Error has occurred.")
End Try
Try
DoSomething
Catch E As EArgumentException
MsgBox("Invalid argument")
Catch E As EOutOfMemory
MsgBox("Out of memory")
Catch
MsgBox("Other error")
End Try
The variable, specified in a Catch block becomes accessible within the block and holds the reference to the current exception instance:
Try
DoSomething
Catch E As EArgumentException
LogMessage("Error: " & E.Message)
End Try
Normally, if the exception is caught by Catch handler, it will not be re-raised implicitly to parent statements. If you need to re-raise the exception, you can use the Throw statement without arguments:
Try
DoSomething
Catch
MsgBox("Error")
Throw
End Try
This form of the Throw statement is only allowed inside Catch handler. The run-time error will occurs otherwise.
Script code can use the Finally block to guarantee execution of the finalization code in both cases: when error occurred and when it is not:
Dim obj = New TMyObject
Try
obj.DoSomething
Finally
obj.Free
End Try
The obj instance, created in above example will be freed in any case, even if DoSomething will raise an exception. Finally block does not catch the exception, so it will be implicitly re-raiseed away from the current Try statement.
If Exit [Sub/Function/For/Do] statements are used inside the Try statement and leads outside of the Try, the Finally statements will be also executed:
For i = 0 To 10
Try
Exit For
Finally
MsgBox("In finally")
End Try
Next
It is illegal to use Exit [Sub/Function/For/Do] statements inside the Finally block itself; run-time error will be raised:
For i = 0 To 10
Try
DoSomething
Finally
Exit For 'Illegal!
End Try
Next