< Back

Document Subject: Golden Rule No7 Don't use On Error Resume Next
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#GR7 or http://A555F9/nn.nsf/ByAlias/GR7

I hate On Error resume next. What does it do? It is basically a lazy programmer's way of handling errors in code. It tells the error handler to move on to the next line when there is an error. This can make sloppy code look like it sort of works but it can have catastrophic effects and is also a complete pain to debug.




When I have to fix other people's code, the first thing I do is comment out the On error resume next line, or replace it with some nice error handling code:

example:

On Error Goto ErrHdlr

'main code of routine

End

ErrHdlr:

' Put in some general error handling routines if necessary

If err=PossibelErrNo ' directory does not exist

 ' do something

else ' no specific error founc so do default action

 r=messagebox "Err:" & Err & " " & Error$ & " l: " & erl & " in " & CurFun & " - Try & continue?", 36, "Error"

 if r= 6 then ' user pressed yes

     resume next

 else ' user wants to stop

     print "Error " & Err & "  " & Error$ & " at line " & erl & " in function " & CurrentFunction

     end

 end if ' what did user press

end if ' error check

 

In Web agents you can change use the error handlers to actually stick out useful information which could help debug the code.

Stick the technical information as comments if you do not want to confuse the user.

eg:

print "An error has occurred.<br>"

print "<!-- Error line: " & erl & " -->"

 

NB If you see any on error resumes in my code, let me know I will remove them ASAP.