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.
do
//Your statements here
end
do
//Your statements here
on excep as Java.Exception
//Handle Exception here
end
do
//Your statements here
on exit
//Do something that must be done always, such as
//releasing external resources
end
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
Loops and methods define special-purpose blocks, and these can also be used to handle exceptions.
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.
names as String[]
names = ["John", "Peter", "Mary"]
for each name in names do
// Do something
on ex as Java.Exception
// Handle your exceptions
end
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 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
...
...
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.