VB6中的集中式错误处理 [英] Centralized error handling in VB6
问题描述
公共函数ToError(strClass As String,strMethod As String )As String
错误GoTo errHandle
ToError =Err&错误编号& _
,Src:& Err.Source& _
,Dsc:&错误描述& _
,Project:& App.Title& _
,Class:& strClass& _
,方法:& strMethod& _
,行:& Erl
Err.Clear
退出点:$ b $ b退出函数
errHandle:
oLog.AddToLogToError方法中的错误:& Err.Description,False
恢复exitPoint
结束功能
事实证明因为我在这个函数中声明一个错误处理程序 On Error GoTo errHandle
,VB6在我能够记录之前清除错误。
有没有办法阻止On Error GoTo errHandle语句清除错误?
一个错误
语句将永远清除 Err
变量( Erl
也将重置为0)。在理论上,这意味着您可以通过将 On Error
语句移到 ToString = ...
行(<或删除 ToError
函数中的错误处理程序),但不幸的是,这不一定总是有效。
您的项目引用的每个组件(DLL,ActiveX EXE等)基本上在内存中都有自己的 Err
实例。因此,如果您的 MainApp.exe
引发了一个错误,该错误传递给 ToError
(驻留在单独的 ErrorHandling.dll
),DLL将不会看到您的EXE看到的 Err
变量。他们每个都有自己的私人 Err
变量。
有一个关于问题的至少两种方法,我可以想想:
方法1
As Zian Choy提到的,您可以在 ToError
函数中添加其他参数,一个
代码 / p>
公共函数ToError(_
ByVal strErrSource As String,_
ByVal nErrNumber As Long,_
ByVal sErrDescription As String,_
ByVal nLineNumber As Long)As String
使用示例
然后,您必须像这样从错误处理程序中调用此类代码,将当前 Err
对象,以及 Erl
:
ToError Err.Source,Err.Number,Err.Description,Erl
/ pre>
如果您还想要
App.Title
,您将不得不添加一个附加参数ToError
,因为App.Title
将等于App.Title定义
,而不是引发错误的组件。如果ToError
方法的项目ToError
在另一个项目中,这很重要。
方法2
您可以通过将
Err
对象本身传递为一个更少的详细信息,使您的ToError
函数的参数,但是在这种情况下,您的ToError
函数的第一件事情是立即存储所需的所有相关属性的副本,因为后续的On Error
语句将清除该变量。
代码
$ b $公共函数ToError(ByVal oError As ErrObject,ByVal nLineNumber As Long)As String
'首先复制重要的Err属性'
'在做任何事情之前...'
Dim strErrSource As String
Dim nErrNumber As Long
Dim strErrDescription As String
strErrSource = oError.Source
nErrNumber = oError.Number
strErrDescription = oError.Description
错误Goto errHandle
'更多代码这里
'...
使用示例
ToError Err,Erl
I have the following method that all the error handlers call:
Public Function ToError(strClass As String, strMethod As String) As String
On Error GoTo errHandle
ToError = "Err " & Err.Number & _
", Src: " & Err.Source & _
", Dsc: " & Err.Description & _
", Project: " & App.Title & _
", Class: " & strClass & _
", Method: " & strMethod & _
", Line: " & Erl
Err.Clear
exitPoint:
Exit Function
errHandle:
oLog.AddToLog "Error in ToError Method: " & Err.Description, False
Resume exitPoint
End Function
It turns out that because I declare an error handler in this function On Error GoTo errHandle
, VB6 clears the error before I am able to record it.
Is there a way to prevent the 'On Error GoTo errHandle' statement from clearing the error?
An On Error
statement will always clear the Err
variable (Erl
will also be reset to 0). In theory this means you could fix the problem by moving the On Error
statement below the ToString = ...
line (or removing the error handler in the ToError
function altogether), but unfortunately that won't necessarily always work either.
Each component (DLL, ActiveX EXE, etc.) referenced by your project essentially gets its own Err
instance in memory. So, if your MainApp.exe
raises an error which gets passed to ToError
(residing in a separate ErrorHandling.dll
for example), the DLL won't see the Err
variable that your EXE sees. They each have their own private Err
variables.
There are a at least two ways around the problem that I can think of:
Method 1
As Zian Choy mentions, you could add additional parameters to your ToError
function, one for each property of the Err
object that you need access to.
Code
Public Function ToError( _
ByVal strErrSource As String, _
ByVal nErrNumber As Long, _
ByVal sErrDescription As String, _
ByVal nLineNumber As Long) As String
Example usage
You would then have to call like this from your error handlers like so, passing it all the relevant values from the current Err
object, along with Erl
:
ToError Err.Source, Err.Number, Err.Description, Erl
If you also want App.Title
, you're going to have to add an additional parameter to ToError
for that as well, since App.Title
will be equal to the App.Title
of the project where the ToError
method is defined, not the component where the error was raised. This is important if ToError
is in a different project.
Method 2
You can make your ToError
calls a little less verbose by passing the Err
object itself as a parameter to the function, however the first thing your ToError
function should do in this case is immediately store a copy of all the relevant properties you need since a subsequent On Error
statement will clear the variable.
Code
Public Function ToError(ByVal oError As ErrObject, ByVal nLineNumber As Long) As String
'Copy the important Err properties first, '
'before doing anything else... '
Dim strErrSource As String
Dim nErrNumber As Long
Dim strErrDescription As String
strErrSource = oError.Source
nErrNumber = oError.Number
strErrDescription = oError.Description
On Error Goto errHandle
'More code here
'...
Example Usage
ToError Err, Erl
这篇关于VB6中的集中式错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!