Defines a final end block for any ABL block, which contains code that must run regardless of an error or STOP condition. An end block is an ABL block that can occur only within another block. The block containing the end block is known as the associated block. End blocks must occur between the last line of executable code in the associated block and its END statement.
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 may include code to delete dynamic objects, write to logs, close outputs, and other routine tasks.
A FINALLY block will run on each iteration of a block, even if that iteration resulted in an error or STOP condition.
This is the syntax for the FINALLY statement and its related blocks:
As shown in r-finally01.p, the FINALLY block executes before any flow-of-control (LEAVE, NEXT, RETRY, RETURN, or THROW) options are executed for the associated block. For iterating blocks, the FINALLY block executes after each iteration of the block.
r-finally01.p
DO ON ERROR UNDO, LEAVE:
/* Since Customer 1000 does not exist, the FIND statement raises ERROR and
execution goes to the FINALLY block before the LEAVE option executes. */
FIND Customer WHERE CustNum = 1000.
MESSAGE "This message never appears because of ERROR condition."
VIEW-AS ALERT-BOX BUTTONS OK.
FINALLY:
MESSAGE "Inside FINALLY block." VIEW-AS ALERT-BOX BUTTONS OK.
/* LEAVE DO block here */
END FINALLY.
END. /* DO */
MESSAGE "Out of DO block." VIEW-AS ALERT-BOX BUTTONS OK.
|
Output:
** Customer record not on file (138) Inside FINALLY block. Out of DO block. |
In r-finally02.p, after ERROR is raised, execution goes to the CATCH block and then to the FINALLY block.
r-finally02.p
DO ON ERROR UNDO, LEAVE:
/* Since Customer 1000 does not exist, the FIND statement raises ERROR and
execution goes to CATCH block. */
FIND Customer WHERE CustNum = 1000.
MESSAGE "This message never appears because of ERROR condition."
VIEW-AS ALERT-BOX BUTTONS OK.
CATCH eSysError AS Progress.Lang.SysError:
/* Handler code for SysError condition */
MESSAGE "Inside CATCH block." VIEW-AS ALERT-BOX BUTTONS OK.
/* Execution goes to FINALLY before leaving DO block. */
END CATCH.
FINALLY:
/* Your code */
MESSAGE "Inside FINALLY block." VIEW-AS ALERT-BOX BUTTONS OK.
/* LEAVE DO block here. */
END FINALLY.
END. /* DO */
MESSAGE "Out of DO block." VIEW-AS ALERT-BOX BUTTONS OK.
|
Output:
Inside CATCH block. Inside FINALLY block. Out of DO block. |