Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try...Catch...Finally - how to use?

Status
Not open for further replies.

StewartUK

Programmer
Feb 19, 2001
860
GB
This follows on from thread1251-924215

I am using VFP8 but haven't really got to grips with TRY...CATCH...FINALLY (TCF).

Here is a snippet of code (there is more code before and after this bit):
Code:
IF NOT TextCopied
   oWord = CREATEOBJECT("Word.Application")
   WAIT WINDOW NOWAIT "Mailmerge - copying text"
   oDoc = oWord.Documents.Open(THIS.txtLetter.Value)
   oRange = oDoc.Range()
   oRange.Copy()
   CopiedText = _CLIPTEXT
   && ....assign the clipboard contents to a variable
   &&     to prevent problems should the user copy
   &&     some text to the clipboard in another application
   TextCopied = .T.
ENDIF

The line oDoc = oWord.Documents.Open(THIS.txtLetter.Value) sometimes causes an error.

To implement TCF, should I:

[ol][li]put the whole IF...ENDIF inside the TRY and, if it caused an error, set a variable to FALSE and test that variable in the line beyond the TRY...ENDTRY to see if the rest of the code should execute[/li]
[li]put just the offending line inside the TCF structure[/li]
[li]put the preceding lines and all the lines that follow inside the TCF structure?[/li][/ol]

Thanks,

Stewart
 
Everyone has their own style, but here's how I'd handle this situation:
Code:
IF NOT TextCopied
   TRY
     WAIT WINDOW NOWAIT "Mailmerge - copying text"

     oWord = CREATEOBJECT("Word.Application")
     oDoc = oWord.Documents.Open(THIS.txtLetter.Value)
     oRange = oDoc.Range()
     oRange.Copy()
     CopiedText = _CLIPTEXT
     && ....assign the clipboard contents to a variable
     &&     to prevent problems should the user copy
     &&     some text to the clipboard in another application
     TextCopied = .T.
  CATCH TO oExc
    messagebox('There was a problem: '+oExc.Message,0,'Error')
  FINALLY
    RELEASE oRange
    RELEASE oDoc
    RELEASE oWord
  ENDTRY
  * if it wasn't successful, TextCopied never was set .T.
ENDIF

It really all depends on where you want user interface.

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top