It's OKAY to check LAN availability by
Code:
cFile = "\\sharename\yourappdata\yourapp.dbc" && filename of a file you know must exist on a mapped network drive or file share (UNC path)
IF FILE(cFile)
But please don't take "\\sharename\yourappdata\yourapp.dbc" literally, you have to adapt that to your situation.
The example is the file name of the application dbc that - no matter how many upgrades you plan in the future - will stay the same.
The only downside to that is that not finding the file because of a network outage could have long timeout, even something in the magnitude of a minute is possible. And you'd want to prevent that to happen. Therefore again actually the first step should be using some API call just like for the internet connection. Let me target that later.
Using TRY..CATCH is indeed, as you say, something that can't hurt. But I'd not leave this uncommented for putting FILE() into a TRY block. That's not helpful, because in short FILE() correctly parameterized will return .F. and not trigger an error and the CATCH block. FILE() will only trigger an error,...
1. ...if you call it with no parameter (Error 1229) or more than 2 parameters (Error 1230)
2. ...if you call it with a wrong parameter type, for example a number as filename (Error 11)
and maybe some more conditions. But in short rather a programming error, a programmer error, not an error that happens at runtime. Would VFP be strictly typed it would be errors that the compiler could already point out.
To get back to "You can always use TRY..CATCH, it causes no harm": Yes, but if you want to cover general errors you better use ON ERROR handling or in case of catching class specific errors use the Error() event of a class. TRY..CATCH isn't a bad idea in itself, though.
For making a good use of TRY...CATCH, you should try (literally put in the TRY block) something that you expect to throw an error, if it fails. That could be (to stay with my example of the DBC file):
Code:
Local llNetworkavailable
TRY
OPEN DATABASE \\sharename\yourappdata\yourapp.dbc
CATCH
* unfortunately there's nothing to enforce the availability of the network
* so nothing more to do here
FINALLY
llNetworkavailable = DBUSED('yourapp')
ENDTRY
IF llNetworkavailable
? 'LAN is up, the database is opened'
Else
? 'The DBC couldn't be opened, maybe a network outage.'
Endif
But even that isn't really ideal, because you could also trigger an error, if the LAN connection exists, but the DBC is open exclusive by another user or other conditions that are not related to a network outage. So it would help to check what error triggered the CATCH block to run. One way to specifically only react to the case of a network outage would be using the CATCH TO Exceptionvariable FOR variant, that limits which errors to handle with the CATCH block, i.e. not the error caused by exclusive access elsewhere.
Now let me target how to use an API for that. Since Vista the core API to use would be NLM - The Network List Manager. That would also cover the internet connection detection. But unfortunately there is no easy way in for VFP, neither with a DECLARE nor CreateObject().
I also don't find any examples about usage of it. Therefore, in short, you may use the FILE() solution by Steven. Check out what happens when you remove the LAN cable or turn off WLAN about the timeout behavior of FILE(). If it answers .F. due to no LAN connection quite as fast as it answers .T. when the LAN is connected, then there is no reason to go for something more complicated. Removing the LAN cable is an easy way to force the condition that would otherwise also come up even though the cables are all connected. It's also not the ideal way to test network outage, but that topic is quite complex and mocking a condition like a network outage isn't very straight forward to cover all special cases there could be. So it also helps logging what happens, which error numbers occur etc. and then have more information when the real LAN outage case happens. Which in essence speaks for an ON ERROR handler that logs error information into an error log file, text or dbf is your choice. No idea if you already have error handling established, it's a very basic thing to have in any applcation code.
Chriss