Compound Statement

A compound statement groups other statements in a logical unit. It defines a scope and allows you to handle exceptions or execute statements that must always be executed, regardless of the outcome of the block.

Syntax

The most basic form of a compound statement simply encloses some statements together. This is also called a code block:
do
    //Your statements here
end
You can also handle exceptions that are thrown inside the block:
do
    //Your statements here
on excep as Java.Exception 
    //Handle Exception here
end
You can add some code to be executed when your block finishes, regardless of whether it has finished by exception or not:
do
    //Your statements here
on exit
    //Do something that must be done always, such as
    //releasing external resources
end
Exception and end of block handling can be combined. For example:
do
    //Your statements here
on ex as Java.Exception 
    //Handle Exception here
on exit
    //Do something that must always be done, such as
    //releasing external resources
end

About Loops and Methods

Loops and methods define special-purpose blocks, and these can also be used to handle exceptions.

Loops

Loops are used to execute a given code block several times, usually with one or more variables which hold different values in each iteration of the loop. These values may be updated by the code block within the loop, or as a result of the loop definition itself.

For example, the following loop will iterate three times, where the name variable will successively adopt the values of "John", "Peter", and "Mary":
names as String[]
names = ["John", "Peter", "Mary"]
for each name in names do
    // Do something
on ex as Java.Exception
    // Handle your exceptions
end
which is equivalent to:
names as String[]
names = ["John", "Peter", "Mary"]
do
    for each name in names do
        // Do something
    end
on ex as Java.Exception
    // Handle your exceptions
end

Methods

Methods define a compound statement that contains the entire body of the method. Each method is contained by an implicit do/end block. For example, the following statement:

on Exception

is semantically equivalent to:

          do
          on Exception
          end
      
This block is labeled after the method's name, so you can add exit and exception handlers directly in the method as in the following:
    ...
    ...
    on ex as Java.Exception
       //Handle Exception here
    on exit
       //Execute required code, such as releasing external resources
    ...

The choice between using an explicit do/end block or handling exceptions directly within a method depends on the scope of the method you are writing. Also, code readability should be taken into account when choosing which style to use.