The following example illustrates how to code a transaction, which is an all-or-nothing attempt at multiple database changes. If any of the changes should fail—for example, attempting to write a new record to disk, which would fail if there was no more disk space—the entire transaction must be rolled back:

try

form.rowset.parent.database.beginTrans( ) // Begin the transaction 

// 

// make changes 

// 

form.rowset.parent.database.commit( ) // If you got this far, there were no 

// errors, so commit the changes 

catch ( Exception e ) // The parameter receives the Exception object that describes

// the error (not used in this example, but required) 

form.rowset.parent.database.rollback( ) // Undo any changes that did take 

// display an error message 

endtry

This example runs a process in a subdirectory, the name of which is passed as the parameter cDir. It uses two TRY blocks to create the subdirectory if necessary, and return to the previous directory even if there is an error in the process:

try

// Instead of bothering to see if the directory already exists 

md &cDir // Go ahead and try to create the directory 

catch ( Exception e ) // If there's an error creating the directory,

// execution goes here. 

// Do nothing -- this assumes the error is because the directory already exists. 

// By using a CATCH, the error is ignored. 

finally

cd &cDir // Now try and go to that directory 

// At this point, if you can't go to the directory, then that's a real error. 

// That would be handled normally, since the error occurred in the FINALLY and 

// is not nested inside another TRY. 

endtry

try

// 

// Run the process 

// 

// No CATCH, so if there's an error, there will be a dialog

finally

// But because of this FINALLY, the previous directory will be restored regardless. 

// This makes the code easier to re-test, since you don’t have to switch back to 

// your main directory manually after an error. 

cd .. 

endtry

Note that in the first TRY block, the statement to switch to the subdirectory doesn’t have to be in a FINALLY block. Unlike the second TRY block, where the FINALLY will switch back to the parent directory even if there is an error, the switch to the subdirectory would work just as well between the two TRY blocks. It’s shown in the FINALLY as an example of what would happen if there is an exception in a FINALLY block.