TRY

From Recital Documentation Wiki
Revision as of 16:26, 25 November 2009 by Helengeorge (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Purpose

Structure to handle errors and exceptions within a block of code


Syntax

TRY

[<tryCommands>]

[CATCH [TO <memvar>] [WHEN <expL>]

[<catchCommands>]]

[THROW [EXCEPTION] [<exp>]]

[EXIT]

[FINALLY

[<finallyCommands>]]

ENDTRY

See Also

ERROR(), MESSAGE(), ON ERROR


Description

TRY...CATCH...FINALLY is a command structure to handle errors and exceptions within a block of code. The <tryCommands> which follow the TRY statement are executed. If no error occurs in the <tryCommands> program execution continues from the FINALLY statement. If an error occurs, program execution jumps immediately to the CATCH statement.

CATCH

The CATCH block with its <catchCommands> handles the error. If the optional TO <memvar> clause is specified, a reference to an Exception object is stored to <memvar>. Please see the table below for Exception Class properties. If the optional WHEN <expL> clause is specified, the <expL> is evaluated and must be equal to True (.T.) for the <catchCommands> to be processed.

THROW

The THROW command causes an error to be thrown. This is primarily used to escalate an error to an outer TRY...CATCH...FINALLY structure. If the THROW is not in a nested TRY...CATCH...FINALLY structure, the error is handled by the active ON ERROR error handler or if none is active, by the default Recital error handler, which will generate an error.mem error log. The optional <exp> is used to specify the UserValue property of a new Exception object.

EXIT

The EXIT command is used to break out of the structure and execution continues from the FINALLY block or after the ENDTRY if no FINALLY block exists or the EXIT is called from the FINALLY block itself.

FINALLY

The FINALLY block <finallyCommands> are run unless a CANCEL or QUIT has been used to exit the structure. It can be used to clean up or release any resources used in the TRY or CATCH blocks.

ENDTRY

The ENDTRY statement completes the structure.

Exception Class

PROPERTY DESCRIPTION
BaseClass The System base class: 'Exception'.
Class The class: 'Exception'.
ClassLibrary The class library: for System classes.
Comment Descriptive text string.
Details Additional details relating to the Message property value.
ErrorNo The error number for the last error that occurred (same as ERROR()). If the Exception object is created by a THROW <exp>, ErrorNo is 2071.
LineContents The line that caused the error (same as MESSAGE(1)).
LineNo The number of the line that caused the error (same as LINENO()).
Message The error message (same as MESSAGE()).
Name The name used to reference the object: 'EXCEPTION'.
Parent The parent object.
ParentClass The parent object class.
Procedure The name of the procedure in which the error occurred.
StackLevel The stack level at which the error occurred.
Tag Used to store any additional string data required.
UserValue The value passed by the THROW statement, which can be used as an object reference.


Example

try
    use example exclusive
catch
    dialog box [Unable to open example table]
endtry
 
//Another example
try
    use example exclusive
catch to oExc
    if oExc.message = "ALIAS name already in use"
        select example
        exit
    else
        dialog box [Unable to open example table]
    endif
endtry
 
//Another example
try
    ? [Outer Try]
    try 
        use example exclusive
    catch to oExc 
        oExc.UserValue = "Nested CATCH message: Unable to handle"
        ?[: Nested Catch]  
        ?[  Inner Exception Object: ]
        ?[  Error: ] + str(oExc.ErrorNo) 
        ?[  LineNo: ] + str(oExc.LineNo) 
        ?[  Message: ] + oExc.Message 
        ?[  Procedure: ] + oExc.Procedure 
        ?[  StackLevel: ] + str(oExc.StackLevel) 
        ?[  LineContents: ] + oExc.LineContents
        ?[  UserValue: ] + oExc.UserValue 
        throw oExc 
    finally
        ?[: Nested FINALLY executed ] 
    endtry   
catch to oExc1
    ?[: Outer CATCH ]
    ?[  Outer Exception Object: ] 
    ?[  Error: ] + str(oExc1.ErrorNo)
    ?[  LineNo: ] + str(oExc1.LineNo) 
    ?[  Message: ] + oExc1.Message 
    ?[  Procedure: ] + oExc1.Procedure 
    ?[  StackLevel: ] + str(oExc1.StackLevel) 
    ?[  LineContents: ] + oExc1.LineContents 
    ?[  ->UserValue becomes inner exception THROWn from nested TRY/CATCH ]
    if oExc1.UserValue.Message = "ALIAS name already in use"
        select example
    endif    
finally
?[: FINALLY executed ] 
endtry


Products

Recital Server, Recital