Home
Do you always avoid On Error Resume Next? (VB Only)
  v4.0 Posted at 8/09/2014 10:14 AM by Drew Robson

​​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 have a program called SSW Code Auditor to check for this rule.

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments: