Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
I want an error for VFP Open dialog
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00885697
Message ID:
00887152
Views:
25
Defensive coding also means to be prepared for things that should never happend without polluting the code with defensive code.

After i call the MySqlExec() function there is no need to test if the cursor is opened because i know this function will throw an exception if it can open the cursor. Should it be the same with any function ?

Lets expand my example a bit with the defensive code
Try
  MySqlExec(myHandle, "Select * from Sometable", "MyCursor")
  RaiseEvent(this, "DataArrival", "MyCursor")
  If Used("MyCursor")
    Select Count(*) From MyCursor Into Array aMyCount
    RaiseEvent(this, "DataArrivalCount", aMyCount)
    IF Used("MyCursor")
        RaiseEvent("this", "AnotherEvent" )
        If Used("MyCursor") 
           Select MyCursor  
           Scan
              ...       
           EndScan
         Endif
    Endif
  Endif
  ...
Catch
...
EndTry
IF VFP would throw an exception instead of showing an open dialog i could code like this:
Try
  MySqlExec(myHandle, "Select * from Sometable", "MyCursor")
  RaiseEvent(this, "DataArrival", "MyCursor")
  Select Count(*) From MyCursor Into Array aMyCount
  RaiseEvent(this, "DataArrivalCount", aMyCount)
  RaiseEvent("this", "AnotherEvent" )
  Select MyCursor  
  Scan
     ...       
  EndScan
  ...
Catch
...
EndTry
Which one is better to you ?

>I understand. But would it not be better for your user to plan design and test before we deleiver - as opposed to TRYING TO CATCH afterwards?
>
>>>Fix your bugs - do ask VFP to help you hide them!
>>
>>Terry,
>>
>>It sounds like you don't get it. Noboby is asking VFP to hide any bug created by a developer. At the contrary, this request is to avoid a possible behavior created by a bug or an external event that may have created the situation which is call an Exception
>>
>>Take this little piece of code for example:
>>
>>Try
>>  MySqlExec(myHandle, "Select * from Sometable", "MyCursor")
>>  RaiseEvent(this, "DataArrival", "MyCursor")
>>  Select Count(*) From MyCursor Into Array aMyCount
>>  ...
>>Catch
>>...
>>EndTry
>>
>>
>>The function MySqlExec if not successfull with throw an exception wich i will handle in the catch section. So would it be ok to assume the call to "select count(*)" should be successful? No! Not at this time because if some programmer hook to the dataArrival event and close the cursor MyCursor, VFP will show an "open" dialog box. Dho! Open what? it does not say. And even if it would say, there nothing to open because this cursor comes from an SQL Server database...
>>
>>What a lot of people in this tread are saying is: code defensively and i agree. So before the call to select count(*) i would add a If Used("MyCursor"), This is just not natural and makes the code less readable.
>>
>>I would find more natural not adding an if used() line and let an exception to occured, then i can deal with it in the catch section.
>>
>>That's what exception handling is all about. Code naturally and an prepare for exception at the right place, that is: in the catch section.
>>
>>Stephane
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform