xHarbour Reference Documentation > Statement Reference |
Declares a control structure for error handling.
TRY <statements> [THROW( <oErrorObject> )] [CATCH [<thrownError>] <errorHandling> [FINALLY <guaranteed> ] END
The statements following the CATCH clause are used for error handling.
Any code that might throw an exception is placed inside of the try block. If an exception is thrown, the catch block is entered and the program can perform the appropriate operation to recover or alert the user.
If FINALLY is specified, code within the FINALLY section is guranteed to be executed after the TRY section has been executed and the CATCH section is activated, unless the CATCH section throws an UNHANDLED Error. This means that the FINALLY section will be executed even if the CATCH section re-throws the error or attempts to RETURN. In such cases, the requested operation which forces out of the TRY section will be deferred until after the FINALLY section has been completed.
Important: although CATCH and FINALLY are both marked as optional, at least one of these options must be used within a TRY...END control block.
See also: | BEGIN SEQUENCE, Error(), ErrorBlock(), ErrorNew(), Throw() |
Category: | Control structures , Statements , xHarbour extensions |
// The example demonstrates program flow for a TRY..CATCH sequence PROCEDURE Main() LOCAL oErr TRY ? "Trying" ? "Throwing" Throw( ErrorNew( "Finalize Test", 0, 0, "Forced Error" ) ) CATCH oErr ? "Caught:", oErr:Operation ? "Throwing to outer, should be deferred" Throw( oErr ) FINALLY ? "Finalized" END ? "Oops, should have Re-Thrown, after the FINALLY." RETURN
http://www.xHarbour.com