dcsimg
Results 1 to 11 of 11

Thread: [RESOLVED] How to handle errors with On Error Resume Next

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    Resolved [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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,032

    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.

    Do I need "Err.Clear"?
    As it is just before a Resume, no you don't.

  3. #3
    Lively Member
    Join Date
    Mar 2011
    Posts
    118

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    Re: How to handle errors with On Error Resume Next

    Quote Originally Posted by Foolish Tech View Post
    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

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    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

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,380

    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.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    18,978

    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.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    Re: How to handle errors with On Error Resume Next

    Quote Originally Posted by dilettante View Post
    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

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,032

    Re: How to handle errors with On Error Resume Next

    Quote Originally Posted by aab1 View Post
    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.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    161

    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
  •  



Featured


Click Here to Expand Forum to Full Width