Use Delete Method To Handle Aborts

This describes a technique for handling unexpected aborts in a LotusScript
program. The specific example is a "FileGuard" class that guarantees that a
file you open gets closed, no matter what else happens.

How it works: whenever you declare a class in LotusScript, you can define a
Delete method that executes when an object of that class is deallocated. This
happens whenever the memory is freed, whether it's because the object has gone
out of scope from the function it was declared in, or because the script
aborted with an error, or by Ctrl+Break.

The simple class below is useful when doing file I/O to make sure that you
don't leave a file open if the user aborts.

Note: you must open a file using the returned file handle from the Handle
method, before you allocate another instance of FileGuard, or else they will
get the same file number. Since Freefile returns the number of the first file
handle that's not yet in use, calling it twice without using the result of the
first call to open a file, returns the same value again.
Class FileGuard
filenumber As Integer
isopen As Integer

Public Property Get Handle
Handle = filenumber
End Property

Sub New()
filenumber = Freefile( )
isopen = True
End Sub

Sub Close
If isopen Then
On Error Resume Next
Close filenumber
isopen = False
End If
End Sub

Sub Delete
End Sub
End Class

Then when you're ready to open a file:

' Keep the next three statements together
Dim FG1 As New FileGuard
outfile% = FG1.Handle
Open "C:\TEMP\DUMP.TXT" For Output As outfile%

Dim FG2 As New FileGuard
infile% = FG2.Handle
Open "\\RAGAMUFFIN\ARGH\TWIST.DAT" For Input As infile%


FG2.Close ' in either order.

This was first published in November 2000

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.