Do you always avoid On Error Resume Next? (VB Only)
Rules for Error Handling|ec2b4be0-f765-46dd-be3e-a5a929ad9126
v4.0
Posted at
8/09/2014 10:14 AM by
Drew Robson
Rule Intro
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.
Page Content
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.
{44FF5D65-56BC-4B02-957D-378C8C0BFC5C}
Do you feel this rule needs an update?