Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Structured Error Handling in VFP 8 : Page 3

With the introduction of Visual FoxPro 3.0, error handling in VFP changed substantially. Rather than using "on error" statements, "state of the art" error events became available. Now, seven years later, more sophisticated error handling mechanisms take center stage as Visual FoxPro 8.0 introduces structured error handling.


advertisement

Conditional Error Handling
Visual FoxPro has a number of functions to retrieve error information, such as Message(). However, those functions are not really adequate to make this bullet-proof, since nested errors make things a bit complicated. For this reason, Microsoft introduced an Exception object. The exception object can be invoked simply by using it on the Catch statement:

   CATCH TO oException
This will make an object named "oException" available within the Catch block. This object has a number of properties, such as ErrorNo, Message, LineNo, Details, LineContents, and more. Using this construct, we can use the following syntax to check for errors caused by the template only:

   FUNCTION Export(lcText1,lcText2)
      LOCAL lReturnValue
      lReturnValue = .T.
      TRY
         * We run the regular code
         LOCAL oWord as Word.Application
         oWord = CREATEOBJECT("Word.Application")
         oWord.Application.Visible = .T.
         TRY
            oWord.Documents.Add("MyTemplate.dot")
         CATCH TO oException
            IF oException.ErrorNo = 1429
               oWord.Documents.Add()
            ELSE
               * We have a different problem
               THROW oException
            ENDIF
         ENDTRY
         oWord.Selection.InsertAfter(lcText1)
         oWord.Selection.InsertAfter(lcText2)
      CATCH 
         lReturnValue = .F.
      ENDTRY
      RETURN lReturnValue
   ENDFUNC
In this example, we handle only error 1429, which is the one that is raised if the template wasn't there. The question is: What do we do with all other errors? Well, basically, we want it to be handled the same way all other errors are handled within the outer Try block. Therefore, we need to elevate the error to that level. We can do so using the Throw statement. This will "re-throw" the error, causing it to be handled by the outer Catch block. (Exceptions elevated using a Throw statement will end up as user exceptions in the outer error handler. See below for more information.)

The WHEN clause of the CATCH statement can utilize any valid Visual FoxPro expression.
This is a pretty simple example. All we really check for is the error number. But, imagine we check for other conditions. For instance, we could try to find another template, or download it from somewhere, and so forth. If all of those attempts fail, we would re-throw the error. If all we wanted to check was the error number, though, we could do something even simpler:



   TRY
      oWord.Documents.Add("MyTemplate.dot")
   CATCH TO oEx WHEN oEx.ErrorNo = 1429
      oWord.Documents.Add()
   ENDTRY
This will catch only error 1429. All other errors will be automatically elevated to the outer error handler, if there is one. Otherwise, the default VFP error dialog would be shown. Therefore, this is a shortcut that is functionally identical to the version shown in the previous example (except that the exception elevated to the outer handler will not be a user error).

What makes this feature very powerful is that there can be a number of different catch blocks:

   TRY
      oWord.Documents.Add("MyTemplate.dot")
   CATCH TO oEx WHEN oEx.ErrorNo = 1429
      oWord.Documents.Add("MyOtherTemplate.doc")
   CATCH TO oEx WHEN oEx.ErrorNo = 1943
      MessageBox("Stop closing Word!!!")
   CATCH
      MessageBox("Something else happened!")
   ENDTRY
Note that Catch blocks are evaluated from top to bottom, and only one of them will run. Therefore, the chosen sequence is important. If we change this example to the following, we would see unexpected (or "expected" after you read this article) results:

   TRY
      oWord.Documents.Add("MyTemplate.dot")
   CATCH
      MessageBox("Something else happened!")
   CATCH TO oEx WHEN oEx.ErrorNo = 1429
      oWord.Documents.Add("MyOtherTemplate.doc")
   CATCH TO oEx WHEN oEx.ErrorNo = 1943
      MessageBox("Stop closing Word!!!")
   ENDTRY
In this scenario, only the first catch block will ever be executed, because it is so generic, it will catch all the errors and the subsequent catch statements will never be evaluated.

The WHEN clause of the CATCH statement can utilize any valid Visual FoxPro expression. Note, however, that to avoid having an erroneous catch statement you shouldn't make these statements too complex.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date