dcsimg
Results 1 to 3 of 3

Thread: Classic VB - What is wrong with using "On Error Resume Next"?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,096

    Classic VB - What is wrong with using "On Error Resume Next"?

    If it is used properly, "On Error Resume Next" is a valid method of dealing with errors - but it is a method that needs to be used appropriately for the situation.

    While using it means that you don't get any 'annoying' error messages that stop your program, it can easily cause much bigger problems than that, such as:
    • If one error happens, then the code after that point is likely to have errors too (especially if the first error occurred while doing something like setting the value of a variable).

      The follow-up error(s) that occur may be simple and 'safe', such as getting the wrong result displayed on screen (but not knowing it is wrong)... or they could be much more serious, such as accidentally deleting an important file, because you had an error getting the name of the file that you wanted to delete.

    • If you have made any coding mistakes (such as putting a text value into an Integer variable) then your code wont do what it is supposed to do, and you wont know why.

      Not only is the problem hidden from you, but so is the location of it. If you are running within VB, you would normally get the line highlighted - but with "On Error Resume Next" you wont. In fact, you wont even know that there was an error, all you will know is that you have made some kind of mistake somewhere in that routine.

      If you donít know what the problem is, or where it is, how will you fix it?
    What will the users of your program think if it gives them the wrong results? ..or does something nasty by mistake? They wont be happy, and they certainly wont think that you are a great programmer!


    As you are reading this article, you probably think that errors are a bad thing, and so hiding them is a good idea. As you get more experience you find that this is not the case, and errors are actually a good thing - they tell you either that you have done something wrong (and why it is wrong), or that something your program relies on isn't working (but you didn't write code to deal with that).

    In either case, VB wants to tell you what the problem is.. but rather than let it tell you (by using proper error handling, or none at all), or deal with the error appropriately (perhaps by exiting the routine), you have decided to put your fingers in your ears and shout 'I'm not listening!'


    So when/how should I use it?
    There are two kinds of situations where it is appropriate:
    1. You really donít care if the code runs properly or not, as it can't do any damage.. you hope!

    2. You are expecting errors in certain part of a routine, but due to the size/structure of the routine (or what that particular piece of code does) it's better to deal with the error where it happened (by checking Err.Number immediately afterwards), rather than in an error handler that for the whole routine.

    If you use the first kind regularly (more than about 10% of your error handling), I'm worried!

    Using the second kind is fine, and is the Ďproperí use. Just make sure that you revert to normal error handling after that piece of code (or have error checks all thru the routine), otherwise you are back to the problems mentioned above.


    In most cases, you should be using proper error handling, as explained in this article.
    Last edited by si_the_geek; Feb 19th, 2008 at 01:30 PM. Reason: added "errors aren't bad" section

  2. #2
    Addicted Member
    Join Date
    Jul 2007
    Posts
    146

    Re: Classic VB - What is wrong with using "On Error Resume Next"?

    As an addition in those situations you want/have to use On Error Resume Next, you can avoid a lot op problems by using the On Error Goto 0 statement.

    Suppose you have a user control and when you dis-enable it, you want all controls on that usercontrol to go to their dis-enabled state as well to gray them out:
    Code:
    Public Property Let Enabled(ByVal Setting As Boolean)
       Dim c As Control
          UserControl.Enabled() = Setting
       
       On Error Resume Next 'If no Enabled property
    
       For Each c In ContainedControls
    
          c.Enabled = Setting
    
       Next
    
       On Error GoTo 0 'Reset normal error trapping.
       
       PropertyChanged "Enabled"
    End Property
    HTH
    Jottum
    Last edited by si_the_geek; Feb 19th, 2008 at 01:12 PM. Reason: formatting of code

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    2,296

    Re: Classic VB - What is wrong with using "On Error Resume Next"?

    I know it's generally bad to reply to 3 year old threads, but since its the FAQ section and I really feel it's missing something...

    If you simply can't change over to the above mentioned better On Error methods, and are encountering a problem and can't trace the source, in the Tools -> Options -> General tab, there's an option to break on all errors which will override your On Error Resume Next statements and show you where the error is happening when you run from the IDE. Hopefully you're not calling IsIDE too much

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