-
May 11th, 2012, 01:11 PM
#1
Thread Starter
Addicted Member
[RESOLVED] How to handle errors with On Error Resume Next
My program uses Inet to connect to the internet, every now and then there's a timeout error which I handle but sometimes the program still manages to crash with a timeout error that for some reason wasn't handled.
This is a server application that absolutely cannot ever crash for absolutely any reason whatsoever. I used to simply have On Error resume next and it would simply ignore all errors, this did make it uncrashable but I wanted to implement a 5 retry feature so that it doesn't just give up on timeout errors.
I'm considering going back to on error resume next but need to know how to handle the timeout errors properly, would it be simply like this:
On Error Resume Next
RetryCnt = 0
Inet.OpenURL ...
If Err.Number = <timeout error> And RetryCnt < 5 Then
RetryCnt = RetryCnt + 1
Err.Clear
Resume
End If
Is that the correct way? Do I need "Err.Clear"?
Thanks
-
May 11th, 2012, 02:01 PM
#2
Re: How to handle errors with On Error Resume Next
That is almost right, but you should ideally have "On Error GoTo 0" (or preferably an error handler) after the End If.
As it is just before a Resume, no you don't.
-
May 11th, 2012, 02:30 PM
#3
Lively Member
Re: How to handle errors with On Error Resume Next
I like to use resume next as well, but use an error handler like so:
Code:
sub whatever()
on error goto errorhandler:
' code goes here
exit sub
errorhandler:
' errorhandling goes here
resume next
end sub
-
May 11th, 2012, 02:38 PM
#4
Thread Starter
Addicted Member
Re: How to handle errors with On Error Resume Next
Originally Posted by Foolish Tech
I like to use resume next as well, but use an error handler like so:
Code:
sub whatever()
on error goto errorhandler:
' code goes here
exit sub
errorhandler:
' errorhandling goes here
resume next
end sub
That's about what I'm using now that's causing the program to crash after several timeout errors except I'm using GoTo Whatever instead of resume next.
Are you also using a global On error resume next and does that handle any unhandled errors in functions and subs?
Thanks
-
May 11th, 2012, 03:15 PM
#5
Thread Starter
Addicted Member
Re: How to handle errors with On Error Resume Next
So I did it like this using on error resume next:
If Err.Number = 35761 And RetryCnt < 5 Then
RetryCnt = RetryCnt + 1
LogData "log.txt", "Request timed out, retry " & RetryCnt
Resume
End If
Where LogData is a Sub I made that logs errors to a log file.
I just have to wait for the next timeout error now but should it work without EVER giving a run time error this way? It's FAR more catastrophic for this app to crash than for errors to get ignored. It's a server app and I have a website set to sound an alarm in my bed room if the app crashes and I got woken up 3 times last night by the alarm as it had crashed with a timeout error. I don't want it to crash and wake me up anymore, especially since it also interrupts the service to my customers.
Thanks
-
May 11th, 2012, 03:28 PM
#6
Re: How to handle errors with On Error Resume Next
Keep in mind that Resume next resumes execution on the next line after the one where the error occurred. In many cases this is the desired behavior but in some you may want it to retry the line where the error occurred which is where you would use resume without the next.
For example say your program is to read data from a CD but the CD is not in the drive so an error occurs. You can prompt the user to put the cd in the drive then execute a resume to have the line where the error occurred execute again as the error has been resolved. Resume Next would skip the line where the error happened so the program would continue on but may not work.
On Error Resume Next should be avoided in almost all cases.
-
May 11th, 2012, 03:37 PM
#7
Re: How to handle errors with On Error Resume Next
Inet works best in general if you use the async Execute method and StateChanged event it provides and not the sync method OpenURL.
However Inet handles FTP pretty clumsily at best even using Execute.
If you are using HTTP/HTTPS then your best bet is probably to use the WinHTTPRequest object (version 5.1). This is much cleaner than Inet, gives you control over timeouts, and is part of Windows Vista/Server 2008 and later.
You can also download WinHTTP 5.1 for installation into Win2k, XP, and Server 2003/2003 R2.
I don't think it's available alone anymore, but now comes bundled as described in An update package that includes BITS 2.0 and WinHTTP 5.1 is available for Windows Server 2003, for Windows XP, and for Windows 2000 for those aging OSs.
-
May 11th, 2012, 03:37 PM
#8
Thread Starter
Addicted Member
Re: How to handle errors with On Error Resume Next
The only lines where an error should ever occur are on the Inet.OpenURL lines where either the server may be slow to respond or the Internet connection could be down.
That's why I only detect for and handle errors after an Inet.OpenURL line and in those cases if the error is a timeout then it will do resume rather than resume next which I want in every other case.
Can Inet.OpenURL generate any other error than a Timeout? That's the only one I ever got, but are there other potential errors from Inet.OpenURL that can be recovered from like the timeout error I handle?
Thank you
-
May 11th, 2012, 03:41 PM
#9
Thread Starter
Addicted Member
Re: How to handle errors with On Error Resume Next
Originally Posted by dilettante
Inet works best in general if you use the async Execute method and StateChanged event it provides and not the sync method OpenURL.
However Inet handles FTP pretty clumsily at best even using Execute.
If you are using HTTP/HTTPS then your best bet is probably to use the WinHTTPRequest object (version 5.1). This is much cleaner than Inet, gives you control over timeouts, and is part of Windows Vista/Server 2008 and later.
You can also download WinHTTP 5.1 for installation into Win2k, XP, and Server 2003/2003 R2.
I don't think it's available alone anymore, but now comes bundled as described in An update package that includes BITS 2.0 and WinHTTP 5.1 is available for Windows Server 2003, for Windows XP, and for Windows 2000 for those aging OSs.
Thanks, but I need a control that handles cookies as I need to log into a site that requires cookies. I believe WinHTTPRequest doesn't handle cookies like Inet does which is why I use Inet, it's been extremely reliable for me with the only errors ever being caused by slow server response or the Internet connection being down, never an error caused by Inet itself.
Thanks
-
May 12th, 2012, 09:06 AM
#10
Re: How to handle errors with On Error Resume Next
Originally Posted by aab1
So I did it like this using on error resume next:
If Err.Number = 35761 And RetryCnt < 5 Then
RetryCnt = RetryCnt + 1
LogData "log.txt", "Request timed out, retry " & RetryCnt
Resume
End If
Where LogData is a Sub I made that logs errors to a log file.
That looks good to me, but make sure that you also log errors after the End If (assuming Err.Number <> 0), so that you are aware of any other issues that occur.
Arguably it would be better to only log errors after the End If, as that way you don't get informed of every instance of a timeout error, but only repeated ones. Of course that depends on what you want.
-
May 12th, 2012, 03:05 PM
#11
Thread Starter
Addicted Member
Re: How to handle errors with On Error Resume Next
Thanks for the help. I can now see in the log it handled several timeout errors and hasn't crashed, seems like this new way of handling errors while still using On Error Resume Next was the key.
I will also be updating it to log more errors as suggested.
Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|