Try OpenEdge Now
skip to main content
Error Handling
Using FINALLY End Blocks : Introduction


In object-oriented programming, the importance of clean-up code that destroys unneeded objects and frees up other resources is vital. The FINALLY statement supports such maintenance tasks. The FINALLY statement creates an end block that executes once at the end of each iteration of its associated block, whether or not the associated block executed successfully or raised the ERROR condition.
The FINALLY block executes after:
*Successful execution of the associated block
*Each successful iteration of an iterating associated block
*ERROR is raised in the associated block and a CATCH block handles the error
*ERROR is raised in the associated block and no CATCH block handles the error
The FINALLY block will not execute if:
*A STOP condition is raised and not handled
*A QUIT statement is in effect and it is not handled
There can only be one FINALLY block in any associated block. The FINALLY statement must come after all other executable statements in the associated block. If the associated block contains CATCH statements, the FINALLY block must come after all CATCH blocks. Note that the FINALLY statement can be used in a block with no CATCH blocks.
The purpose of a FINALLY block is to hold clean-up code that must execute regardless of what else executed in the associated block. It can include code to delete dynamic objects, write to logs, close outputs, and other routine tasks. Because it executes even if the ERROR condition is raised, the FINALLY block is also a useful part of a structured error handling scheme.
Since a FINALLY block executes after an invoked CATCH block, it can also be used to perform common post-CATCH clean up tasks, rather than repeating common code in all the CATCH blocks present in the associated block.