错误处理程序最佳实践 [英] Error Handler best practices

查看:70
本文介绍了错误处理程序最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用这个约定:


退出_Here:

退出Sub

HandleErr:

选择Case Err.Number

案例3163

继续下一步

案例3376

继续下一步

Case Else

MsgBox"错误号码" &安培; Err.Number& : &安培; Err.Description

简历Exit_Here

结束选择


有没有办法在Case Else中包含当前的程序名?


可能是这样的:


Case Else

MsgBox"错误号码" &安培; Err.Number& : &安培; Err.Description&

vbCrLf& _

Me.ProcedureName& Me.Form

简历Exit_Here

结束选择

(注意:Me.ProcedureName是伪代码 - 我不知道如果有可能

得到这个...)


怎么样将它卸载到模块中所以我不必输入它每个

时间:


Case Else

strP =我!程序

strF = Me .Form

modErr.caseElse

结束选择


想法?建议?


提前致谢...

I use this convention frequently:

Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case 3163
Resume Next
Case 3376
Resume Next
Case Else
MsgBox "Error Number " & Err.Number & ": " & Err.Description
Resume Exit_Here
End Select

Is there a way to include the current procedure name on Case Else?

perhaps something like this:

Case Else
MsgBox "Error Number " & Err.Number & ": " & Err.Description &
vbCrLf & _
Me.ProcedureName & Me.Form
Resume Exit_Here
End Select
(note: "Me.ProcedureName" is pseudo code - I don''t know if it''s possible to
get this...)

How about offloading this to a module so I don''t have to type it out every
time:

Case Else
strP = Me!Procedure
strF = Me.Form
modErr.caseElse
End Select

thoughts ? suggestions ?

Thanks in advance...

推荐答案

RE /
我经常使用这个惯例:

Exit_Here:
退出Sub
HandleErr:
Select Case Err.Number
案例3163
继续下一步
案例3376
继续下一页
Case Else
MsgBox"错误号码" &安培; Err.Number& : &安培; Err.Description
继续Exit_Here
结束选择

有没有办法在Case Else上包含当前的程序名称?

可能是这样的:

Case Else
MsgBox"错误号码" &安培; Err.Number& : &安培; Err.Description&
vbCrLf& _
Me.ProcedureName& Me.Form
Resume Exit_Here
结束选择
(注意:Me.ProcedureName是伪代码 - 我不知道是否有可能获得
这个...)

如何将它卸载到模块中,这样我就不必每隔一段时间输出一次:

Case Else
strP =我!程序
strF = Me.Form
modErr.caseElse
结束选择

想法?建议?

提前致谢...
I use this convention frequently:

Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case 3163
Resume Next
Case 3376
Resume Next
Case Else
MsgBox "Error Number " & Err.Number & ": " & Err.Description
Resume Exit_Here
End Select

Is there a way to include the current procedure name on Case Else?

perhaps something like this:

Case Else
MsgBox "Error Number " & Err.Number & ": " & Err.Description &
vbCrLf & _
Me.ProcedureName & Me.Form
Resume Exit_Here
End Select
(note: "Me.ProcedureName" is pseudo code - I don''t know if it''s possible to
get this...)

How about offloading this to a module so I don''t have to type it out every
time:

Case Else
strP = Me!Procedure
strF = Me.Form
modErr.caseElse
End Select

thoughts ? suggestions ?

Thanks in advance...




我写的每一个例程都在下面的骨架内。


DebugStackPush(),DebugStackPop()和BugAlert()。全部在

a模块中我称之为basBugAlert。


Push / Pop例程将例程的名称推入数组/弹出。


" BugAlert"指的是数组,以便在错误弹出之前找到我们已经在哪里
。然后它会向用户显示一个小错误屏幕,并将错误和跟踪记录在.TXT文件中。


模块结束时这个说明。如果有人能说得更好一点,我会很感激改进代码的副本。


如果你想编译它并且换行会让你发疯,发布

a回复,我会将.bas文件通过电子邮件发送给你。


------ -----------------------------------------

无论如何()

DebugStackPush mModulename& ":Whatever"

On Error GoTo Whatever_err


''目的:做任何事情

''ACCEPTS:

''退货:

''

''注意:1).....

(代码去这里......)


Whatever_xit:

DebugStackPop

错误继续下一步

(发布指针,关闭记录集)

退出Sub


Whatever_err:

BugAlert True,"

(如果某些错误可以接受,可以选择在Err上结束)

恢复Whatever_xit

------------------ ----------------------------

选项比较数据库''使用数据库顺序进行字符串比较
选项明确


''此模块包含用于捕获/记录错误的例程和

''显示bugAlert屏幕。


''要求:1)一个名为---------- Program changes ----------的表格在应用程序中

''

''2)全局常量:

''Global Const gIniGroupName =" TretsParms"

''

''3)两种形式:

''frmBugAlertConcise

''frmBugAlertVerbose

''

''注意:1)为了避免循环,大多数这些例程应该使用自己的

自己的错误捕获,

''仅限于显示消息框 - 而不是

到潜在的递归

''调用debugStackPush()和debugStackPop()


Const mModuleName =" basBugAlert"


全局Const gStackLimit = 50


全局gDebugStack(debugStackTotalSize)

全局gStackPointer作为整数

全局gErrorMessage作为字符串''对于任何调用例程

想要在bugAlert关闭之前捕获错误消息。

全局gErrorLocation As String''同上,但包含

例程名称


私有声明函数GetComputerName_bal Lib" kernel32" Alias

" GetComputerNameA" (ByVal lpBuffer As String,nSize As Long)As Long

私有声明函数GetUserName_bal Lib" advapi32.dll"别名GetUserNameA

(ByVal lpBuffer As String,nSize As Long)As Long

私有声明函数GetPrivateProfileString Lib" kernel32" Alias

" GetPrivateProfileStringA" (ByVal lpApplicationName As String,ByVal lpKeyName

As Any,ByVal lpDefault As String,ByVal lpReturnedString As String,ByVal nSize

As Long,ByVal lpFileName As String)As Long < br $>
Sub bugAlert(ByVal theDisplaySwitch As Integer,ByVal theSupplementalMessage As

String)


''目的:记录错误,也许,向用户显示一个错误屏幕

''ACCEPTS: - 一个告诉用户是否向用户显示屏幕的布尔值

'' - 补充文字要添加到日志条目并显示在

屏幕上

''用途: - 一个名为myErrorPath的可选.INI文件parm,它告诉了哪里

写错误

'' - 一个名为VerboseErrorDisplay的可选.INI文件parm如果我们想要

''来表示frmBugAlertVerbose

''

''注意:1 )我们处于错误模式:任何事情都可能发生。

''因此错误捕获仅限于消息框。

''2)我们假设调用例程,在调用之后,将优雅地继续

''到它''退出'"编码并在出路时弹出调试堆栈。

''3)注意出On Error语句不是*之后*我们已经获得了b
$ b捕获的错误信息。

''4)将显示开关设置为False并提供补充

消息允许程序员

''记录错误日志中的内容,这些内容并非技术意义上的

错误。

''例如bugAlert,False,这句话写入错误

log

''5)如果.INI文件中没有指定路径,我们写信给

根C:


1001 Dim myErrorLine As Long

Dim myErrorNumber As Long

将myErrorMessage变暗为字符串


1002 myErrorLine = Erl''尽快捕获相关信息

1004 myErrorMessage =错误



Every routine I write is within the skeleton below.

"DebugStackPush()", "DebugStackPop()", and "BugAlert()" are all in
a module I call "basBugAlert".

The Push/Pop routines push the routine''s name into an array/pop it out.

"BugAlert" refers to the array to get a trace of where we''ve been
just before the error popped. It then displays a little error screen to
the user and logs the error and the trace in a .TXT file.

The module is at the end of this note. If somebody can make it a little
better, I''d appreciate a copy of the improved code.

If you''re trying to compile it and the line breaks are making you crazy, post
a reply and I''ll email the .bas file to you.

-----------------------------------------------
Whatever()
DebugStackPush mModulename & ": Whatever"
On Error GoTo Whatever_err

'' PURPOSE: To do whatever
'' ACCEPTS:
'' RETURNS:
''
'' NOTES: 1).....
(code goes here...)

Whatever_xit:
DebugStackPop
On Error Resume Next
(release pointers, close recordsets)
Exit Sub

Whatever_err:
BugAlert True, ""
(optionally case out on Err if some errors are acceptable)
Resume Whatever_xit
----------------------------------------------
Option Compare Database ''Use database order for string comparisons
Option Explicit

'' This module contains the routines used to trap/log errors and
'' show the "bugAlert" screen.

'' REQUIRES: 1) A table named "---------- Program Changes ----------" in the app
''
'' 2) A global constant:
'' Global Const gIniGroupName = "TretsParms"
''
'' 3) Two forms:
'' frmBugAlertConcise
'' frmBugAlertVerbose
''
'' NOTES: 1) To avoid loops, most of these routines should be using their own
own error trapping,
'' which would be limited to just showing message boxes - as opposed
to potentially-recursive
'' calls to debugStackPush() and debugStackPop()

Const mModuleName = "basBugAlert"

Global Const gStackLimit = 50

Const debugStackTotalSize = 52
Global gDebugStack(debugStackTotalSize)

Global gStackPointer As Integer

Global gErrorMessage As String ''For any calling routine
that wants to trap the error message before bugAlert munches on it.
Global gErrorLocation As String ''Ditto above, but contains
name of routine

Private Declare Function GetComputerName_bal Lib "kernel32" Alias
"GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetUserName_bal Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName
As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize
As Long, ByVal lpFileName As String) As Long
Sub bugAlert(ByVal theDisplaySwitch As Integer, ByVal theSupplementalMessage As
String)

'' PURPOSE: To log an error and, maybe, show an error screen to the user
'' ACCEPTS: - A boolean telling whether-or-not to show a screen to the user
'' - Supplemental text to be added to the log entry and shown on the
screen
'' USES: - An optional .INI file parm called "myErrorPath", which tells where
to write the error
'' - An optional .INI file parm called "VerboseErrorDisplay" that tells
us if we want
'' to show frmBugAlertVerbose
''
'' NOTES: 1) We are in error mode: anything could be happening.
'' Therefore error trapping is limited to a messagebox.
'' 2) We assume that the calling routine, after invoking this, will
gracefully proceed
'' to it''s "Exit" coding and pop the debug stack on the way out.
'' 3) Note that out "On Error" statement isn''t until *After* we''ve
captured error info.
'' 4) Setting the display switch to False and suppling a supplemental
message allows the programmer
'' to record things in the error log which did not result from
errors in the technical sense.
'' e.g. bugAlert, False, "This sentence gets written to the error
log"
'' 5) If there is no path specified in the .INI file, we write to the
root of C:

1001 Dim myErrorLine As Long
Dim myErrorNumber As Long
Dim myErrorMessage As String

1002 myErrorLine = Erl ''Capture relevant info ASAP
1003 myErrorNumber = Err
1004 myErrorMessage = Error




1005 gErrorMessage =错误

1005 gErrorMessage = Error




1006 gErrorLocation = gDebugStack(gStackPointer)


1007错误GoTo bugAlert_err

1008 DoCmd.Echo True''如果它在其他地方被关闭


Dim v As Variant

Dim X As Integer

Dim myMessage As String

Dim myTimeStamp As String

Dim i作为整数

Dim L As Long

将myErrorPath作为字符串调暗

将myHeaderLine调暗为字符串

将myAppVersion作为字符串调暗

将myVerboseSw调暗为布尔值


Dim ParmValue As String


Const cannotDoAtThisTime = 2486


Dim skipLine As String


1010 skipLine = Chr

1006 gErrorLocation = gDebugStack(gStackPointer)

1007 On Error GoTo bugAlert_err
1008 DoCmd.Echo True ''In case it was turned off elsewhere

Dim v As Variant
Dim X As Integer
Dim myMessage As String
Dim myTimeStamp As String
Dim i As Integer
Dim L As Long
Dim myErrorPath As String
Dim myHeaderLine As String
Dim myAppVersion As String
Dim myVerboseSw As Boolean

Dim ParmValue As String

Const cannotDoAtThisTime = 2486

Dim skipLine As String

1010 skipLine = Chr


这篇关于错误处理程序最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆