Secret ingredients to quality software

  • Services
    • Products
      • Training
        • User Group
          • Rules
            • About Us
              • SSW TV

              Do you always avoid On Error Resume Next? (VB Only)

              Last updated by Jake Bayliss about 2 years ago.See history

              This rule has been archived
              Archived Reason: Archiving as this rule as VB isn't relevant anymore

              Never use On Error Resume Next in VB (and VB.NET) projects.

              If an error occurred, On Error Resume Next will hide the error and things can go very haywire! In .NET, stop using the On Error syntax and use the try-catch exception syntax for better structural exception handling.

              In VB/VBA you should use On Error Resume Next with line of comment and after an offending line of code there should be statement On Error GoTo 0 to reset Errors collection.

              Private Sub cmdSelect_Click()
                  Dim varTemp As Variant
                  On Error Resume Next
                  varTemp = columnADOX.Properties("RelatedColumn").Value
                      .
                      ....many lines of code...
                      .
                  intRoutesPerDay = 2
                  End Sub

              Bad Example – Bad code

              Private Sub cmdSelect_Click()
                  Dim varTemp As Variant
                  On Error Resume Next
                  'Sometimes there is no related column value
                  varTemp = columnADOX.Properties("RelatedColumn").Value
                  On Error GoTo 0
              
                  .
                  ....continuing code...
                  .
                  End Sub

              Good Example – Good code

              We open source. Powered by GitHub