Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

Error Handler always stating Error.Number = 0

Hi,

Here is a function that i am trying to us to test an error handler:

Public Sub ProcessPoints(rs As Recordset, _
                         bUseTimeOverride As Boolean, _
                         Optional dteStart As Date, _
                         Optional dteEnd As Date, _
                         Optional bUpdateDate As Boolean = True)
   
    Dim sFunctionName As String
    sFunctionName = "ProcessPoints Testing"
   
    On Error GoTo ErrorHandler
    Err.Clear
   
       
    Dim iResult As Integer
    iResult = 13 / 0
   
    Exit Sub

ErrorHandler:
    'Call oDBG.DBG_Error(mvarm_sClassName, sFunctionName)
    Dim sLine As String
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[ERROR Encountered                                   ]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Class = [" & mvarm_sClassName & "], Function = [" & sFunctionName & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Number = [" & CStr(Err.Number) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Source = [" & CStr(Err.Source) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Description = [" & CStr(Err.Description) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)
   
End Sub


The problem is that the Err.Number is always 0.

Can anyone help me?
0
cossy74
Asked:
cossy74
  • 3
  • 2
1 Solution
 
junglerover77Commented:
I tested your code, and I Just Changed it like this:

Public Sub ProcessPoints(rs As Recordset, _
                         bUseTimeOverride As Boolean, _
                         Optional dteStart As Date, _
                         Optional dteEnd As Date, _
                         Optional bUpdateDate As Boolean = True)
   
    Dim sFunctionName As String
    sFunctionName = "ProcessPoints Testing"
   
    On Error GoTo ErrorHandler
    Err.Clear
   
       
    Dim iResult As Integer
    iResult = 13 / 0
   
    Exit Sub

ErrorHandler:
    Debug.Print Err.Number, Err.Source, Err.Description
End sub

It returns
 11           Project1      Division by zero

So, the Err.Number is NOT 0.
0
 
junglerover77Commented:
I think that why you get Err.Number=0 is because of your method oDBG.DBG_Line.

Because "Err" is a global variable, if you execute a Err.Clear in your oDBG.DBG_Line, the Err.Number will become 0.
0
 
[ fanpages ]IT Services ConsultantCommented:
Also the "Number" property of the "Err" object is also reset (to 0) when another "On Error [Resume Next|Goto To <label>]" statement is encountered.

I would recommend changing you code as follows:

Public Sub ProcessPoints(rs As Recordset, _
                         bUseTimeOverride As Boolean, _
                         Optional dteStart As Date, _
                         Optional dteEnd As Date, _
                         Optional bUpdateDate As Boolean = True)
   
    Dim lngErr_Number As Long  or make this a global (public) variable at the module level so other routines can use it
    Dim strErr_Description As String ' or make this a global (public) variable at the module level so other routines can use it
    Dim strErr_Source As String ' or make this a global (public) variable at the module level so other routines can use it

    Dim sFunctionName As String
    sFunctionName = "ProcessPoints Testing"
   
    On Error GoTo Err_ProcessPoints
'    Err.Clear ' Not needed!
   
       
    Dim iResult As Integer
    iResult = 13 / 0

Exit_ProcessPoints:

    On Error Resume Next

    Exit Sub

Err_ProcessPoints:

    lngErr_Number = Err.Number
    strErr_Description = Err.Description
    strErr_Source = Err.Source

    On Error Resume Next

    'Call oDBG.DBG_Error(mvarm_sClassName, sFunctionName)
    Dim sLine As String
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[ERROR Encountered                                   ]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Class = [" & mvarm_sClassName & "], Function = [" & sFunctionName & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Number = [" & CStr(lngErr_Number) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Source = [" & CStr(strErr_Source) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "Error Description = [" & CStr(strErr_Description) & "]"
    Call oDBG.DBG_Line(sLine)
    sLine = "[----------------------------------------------------]"
    Call oDBG.DBG_Line(sLine)

    Resume Exit_ProcessPoints
   
End Sub


'

BFN,

fp.
0
 
[ fanpages ]IT Services ConsultantCommented:
"I would recommend changing you code..."

or even "youR code".

Sorry.
0
 
[ fanpages ]IT Services ConsultantCommented:
Thanks for closing the question.

BFN,

fp.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now