Overbasic – Try .. Catch

Table of Contents

Correctly handling exceptions (possible errors during script execution) in order to avoid unexpected results, is fundamental in any programming language. In Overbasic you can handle exceptions using the TRY .. CATCH syntax.

Syntax #

Try
    <code block>
Catch
    <code block>
EndTry

The TRY section contains the block of code to be executed.
The CATCH section allows you to define a block of code to be executed, if an error occurs in the TRY block.

In other words, if one of statements in the Try block throws an error, Try block execution ends immediately. Script execution continues with the Catch block.

Usage #

Dim result As Numeric = 0
Dim b As Numeric = 0

Try
    result = 100
    result = result / b 'This statement throws an error because b equals zero
    result = result + 1 'This statement will NOT be executed.
Catch
    result = 2
EndTry

result = result * 10

'The result is: 20

In the example, the execution of the Try block ends with the line result = a / b throwing a divide-by-zero error. Subsequent statements contained in the Try block will NOT be executed. Program execution will continue with the Catch block.