CATCH block syntax and usage
- Last Updated: October 18, 2024
- 2 minute read
- OpenEdge
- Version 12.2
- Documentation
Syntax
|
- object-variable
-
The variable name that references the object caught by this block. Typically, you do not define the object-variable ahead of time with the
DEFINE VARIABLEstatement. The AVM recognizes a new variable name on theCATCHstatement as a new object-variable definition within the current scope. EachCATCHin an associated block must have a unique object-variable. You can reuse an object-variable name in a different associated block, if its type is the same as the previous use. For all blocks with their own variable scope, such as object methods or internal procedures, aCATCHstatement inside that context may reuse the same variable name as aCATCHstatement outside of that context even if the type is different. - [ CLASS ] condition-class
-
Optionally, you can provide the
CLASSkeyword.The code within a
CATCHblock only executes if a condition of type condition-class (or a subtype) is raised within the body of the associated block. When the condition is raised, if there is an active transaction for the associated block, the transaction is undone before the AVM begins executing the statements within theCATCHblock. For more information, see the reference entries for theDEFINE VARIABLEstatement and theTRANSACTIONoption in theDOstatement in the ABL Reference.
Examples
Example 1
In the following example, the CATCH block
handles any ABL system error:
|
Example 2
The following example illustrates reuse of the object-variable name:
|
Example 3
An associated block may have multiple CATCH blocks, each of which handles a different error class. If an error type
satisfies multiple CATCH statements, the AVM executes the
code in the first CATCH block that is compatible with the
error type. It does not execute multiple CATCH blocks.
Therefore, if multiple CATCH blocks are specified, the more
specialized error classes should come first, as shown:
|
Example 4
The compiler issues a warning message if a block contains a CATCH block that is not reachable. The following code produces a
warning, since the CATCH of eMyAppError can never be reached:
|