• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Trapping Internet Connection Errors

Why is it that when I enclose the following statement:

try
hHttpSession:=InternetOpen('WROCSG6', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
except
{do something}
end;

in a try/except statement, an error is not detected even though the computer is not connected to the Internet for example.

The same is true of this statement:

try
hReqURL:=InternetOpenURL(hHttpSession, PChar(URLStr), nil, 0,0,0);
except
{do something else}
end;

When either of these two statements is enclosed within a try/except container, no exception is generated. Why is that? Am I missing something?
0
efz
Asked:
efz
1 Solution
 
jimyXCommented:
Actually the InternetOpen and InternetOpenURL returns a pointer so there is no error to be detected in both cases when it succeeds or fails. So to be able to distinguish between the success and failure of the Internet connection use Assigned:
hHttpSession:=InternetOpen('WROCSG6', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
  if Assigned(hHttpSession) then
    begin
      // succeeded
    end;
finally
  InternetCloseHandle(hHttpSession);
end;

Open in new window


hReqURL:=InternetOpenURL(hHttpSession, PChar(URLStr), nil, 0,0,0);
try
  if Assigned(hReqURL) then
    begin
      // succeeded
    end;
finally
  InternetCloseHandle(hReqURL);
end;

Open in new window

0
 
efzAuthor Commented:
Why didn't I think of that?

I guess that's why you're the expert and I'm the churl.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now