TRY...CATCH...FINALLY Command
Provides a structure to handle errors and exceptions that can occur while running a block of code.
For more information about structured error handling using the TRY...CATCH...FINALLY command, see Structured Error Handling.
TRY
[ tryCommands ]
[ CATCH [ TO VarName ] [ WHEN lExpression ]
[ catchCommands ] ]
[ THROW [ eUserExpression ] ]
[ EXIT ]
[ FINALLY
[ finallyCommands ] ]
ENDTRY |
Parameters
- TRY
-
Indicates the beginning of the TRY...CATCH...FINALLY structure.
- [ tryCommands ]
-
Specifies statements where an error might occur.
- [CATCH]
-
Indicates the beginning of a CATCH block, which handles the appropriate error. You can specify a CATCH statement without arguments, which evaluates to
CATCH WHEN .T.(True).
Tip If you want to have a TRY...CATCH...FINALLY structure that catches all errors, make sure you have a CATCH statement that always evaluates to True (.T.).
You can specify any number of CATCH statements, including none.
- [TO VarName]
-
Specifies a memory variable and not an object reference that stores a reference to the Exception object created. VarName can be only a simple memory variable, not an object property reference.
You can explicitly set VarName to any value in the TRY...CATCH...FINALLY structure. The value does not change as long as another CATCH statement does not run, which resets VarName to NULL.
VarName can have local, public, or private scope, which depends on how the variable is declared. If VarName has not been previously declared, it has private scope. The Exception object created has the same scope as VarName. When VarName goes out of scope, is released, or is reset, the Exception object is also released.
When a CATCH TO VarName clause executes, Visual FoxPro always sets the VarName to a new Exception object regardless of its previous setting, though VarName retains its scope. Therefore, the contents of VarName are always overwritten regardless of what it previously referenced.
- [WHEN lExpression]
-
Specifies an optional logical expression to evaluate when the program encounters the CATCH statement. The expression must evaluate to True (.T.) for the corresponding catchCommands to run.
The argument for lExpression can contain a reference to the VarName Exception object so that you can query the resulting error and handle it accordingly.
If a WHEN clause evaluates to False (.F.), Visual FoxPro releases the Exception object and resets the VarName to NULL. You can test whether VarName is set to a type or to NULL by using the VARTYPE( ) function.
- [ catchCommands ] ]
-
Specifies statements to run when the program encounters the corresponding CATCH statement.
- [THROW [ eUserExpression ] ]
-
Creates a new Exception object to be caught by a CATCH statement. You can call THROW from any code block in a TRY...CATCH...FINALLY structure. However, you can use THROW anywhere in code where an error handler exists to catch the exception. You cannot call THROW from the Command window.
Caution Calling the THROW command outside the TRY...CATCH...FINALLY structure and without an appropriate error handler causes your program to quit.
Note Run-time applications should enclose a THROW command with an outer TRY...CATCH...FINALLY structure. Visual FoxPro disregards any THROW statements appearing in a CATCH WHEN lExpression clause.
The eUserExpression parameter specifies an expression of any type and sets the exception's UserValue property. You can pass an object of any type because the exception's UserValue property stores only the object reference.
When you include eUserExpression with the THROW clause, Visual FoxPro sets the exception's ErrorNo property value to 2071, which corresponds to the error, "User Thrown Error (Error 2071)".
Note You should pass a value for eUserExpression, which sets the exception's UserValue property, so you can chain multiple Exception objects. For more information about chaining Exception objects, see the Example section.
If you omit eUserExpression, Visual FoxPro escalates the original Exception object, if it exists. If the Exception object does not exist, Visual FoxPro throws a new Exception object with an ErrorNo property value of 2071. If you omit eUserExpression, and the THROW clause escalates the Exception object to an outer TRY...CATCH block, the outer TRY...CATCH block catches the same Exception object. Values returned by the ERROR( ), MESSAGE( ), and AERROR( ) functions might not be reliable when used in a structured exception handler, so, you should use the Exception object instead.
If an outer TRY...CATCH statement is in effect when THROW is called from a CATCH or FINALLY block, Visual FoxPro assigns VarName in the CATCH TO clause to the object reference for the Exception object generated by the thrown exception. If no outer TRY...CATCH statement is in effect, Visual FoxPro escalates the exception to an ON ERROR or Error event, if one exists; otherwise, Visual FoxPro calls its system error handler.
If you want to rethrow the original exception that was caught by the CATCH TO VarName clause, you can call THROW VarName to rethrow the exception.
Note Rethrowing the original exception produces another new Exception object, at the outer CATCH statement, that has a UserValue property containing an object reference to the original exception.
If THROW encounters an error when evaluating an expression, Visual FoxPro escalates the error in the usual way.
- [EXIT]
-
Include in a TRY, CATCH, or FINALLY block to immediately stop processing code in a code block.
If EXIT appears in the TRY or CATCH block, the program resumes execution at the FINALLY statement, if one exists. If EXIT appears in the FINALLY block or if FINALLY does not exist, the program resumes execution immediately following the ENDTRY statement.
For more information, see EXIT Command.
- [ FINALLY
-
Indicates the beginning of the FINALLY block, which you use to clean up any resources allocated by the TRY block and always runs except when the CANCEL and QUIT commands are used.
- [ finallyCommands ]
-
Specifies statements to run in the FINALLY block after either finding no errors in the TRY block or running statements in a CATCH block.
- ENDTRY
-
Indicates the end of the TRY...CATCH...FINALLY structure. Each TRY...CATCH...FINALLY structure must end with an ENDTRY keyword. As with other Visual FoxPro control statements, you can add comments prefaced by double ampersand (&&) characters after this ENDTRY.
Note If you do not include the ENDTRY statement at the end of the TRY...CATCH...FINALLY structure, Visual FoxPro generates an error when you compile your code. Although you can still execute code with compile errors, you should not do so in this scenario because it causes Visual FoxPro to stop responding.
Remarks
Example
See Also
© , 1996-2020 • Updated: 11/10/20
Comment or report problem with topic
Microsoft Visual FoxPro 9 SP2 Help file, VFPX Edition v1.08