David Fenton是否正确处理错误? [英] Is David Fenton right about error handling?

查看:51
本文介绍了David Fenton是否正确处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑一般错误处理例程,并编写了一个示例

函数来查找表中的ID。该函数返回True,如果它可以找到ID并根据该ID创建记录集,否则返回

false。


**我不是在寻找关于这个功能的用处的评论 - 它只是为了证明错误处理而是

**


有三个版本这段代码。大卫芬顿说,在早先的

线程中,A97中的DAO特性?版本1是坏的,因为它有On Error Resume Next所涵盖的多个

行,并且存在这样的危险:

''溢出'到另一个代码块。任何人都可以证明这一点吗?

其他人是否有过这种情况的经历?

看来他更喜欢第2版。但我想知道 - 如果我不能依赖
我退出函数时要重置的错误处理
,我可以保证

版本2中Exit_Handler部分出现错误的可能性为零吗?

(例如记录集不是什么,但试图关闭它会导致错误)。

如果Exit_Handler部分出现错误,我们显然会陷入

永无止境的循环,所以在某种程度上确保

这是不可能的。代码也不那么冗长,特别是当有很多对象需要清理时。也许答案是版本3

在最后的'On Error GoTo 0'上进行修复但我从未见过有人用这种类型的错误处理写了一个

函数。


**我尚未决定并寻求小组的意见**


公共函数ContactExists1(lngConID As Long)As Boolean


错误GoTo Err_Handler


Dim dbs作为DAO.Database

Dim rst作为DAO.Recordset

Dim strSQL As String


strSQL =" SELECT tblContact。* FROM tblContact WHERE" &安培; _

" ConID =" &安培; CStr(lngConID)


设置dbs = CurrentDb


设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)


如果不是rst.EOF则

ContactExists1 = True

结束如果


Exit_Handler:

On Error Resume Next

rst.Close

Set rst = Nothing

Set dbs = Nothing

退出函数


Err_Handler:

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

恢复Exit_Handler


结束函数


公共函数ContactExists2(lngConID As Long)As Boolean


错误GoTo Err_Handler


Dim dbs作为DAO.Database

Dim rst作为DAO.Recordset

Dim strSQL As String


strSQL =" SELECT tblContact。* FROM tblContact WHERE" &安培; _

" ConID =" &安培; CStr(lngConID)


设置dbs = CurrentDb


设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)


如果不是rst.EOF则

ContactExists2 = True

结束如果


Exit_Handler:


如果不是第一个没有那么

rst.Close

设置rst =无什么

结束如果


如果不是dbs什么都没有那么

设置dbs =没什么

结束如果


退出函数


Err_Handler:

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

恢复Exit_Handler


结束函数


公共函数ContactExists3(lngConID As Long)As Boolean


错误GoTo Err_Handler


Dim dbs作为DAO.Database

Dim rst作为DAO.Recordset

Dim strSQL As String


strSQL =" SELECT tblContact。* FROM tblContact WHERE" &安培; _

" ConID =" &安培; CStr(lngConID)


设置dbs = CurrentDb


设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)


如果不是rst.EOF则

ContactExists3 = True

结束如果


Exit_Handler:

On Error Resume Next

rst.Close

Set rst = Nothing

Set dbs = Nothing

On Error GoTo 0

退出函数


Err_Handler:

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

恢复Exit_Handler


结束功能

I am considering general error handling routines and have written a sample
function to look up an ID in a table. The function returns True if it can
find the ID and create a recordset based on that ID, otherwise it returns
false.

**I am not looking for comments on the usefulness of this function - it is
only to demonstrate error handling**

There are three versions of this code. David Fenton says under the earlier
thread "DAO peculiarity in A97?" that version 1 is bad since it has multiple
lines covered by On Error Resume Next and that a danger exists of this
''spilling over'' into another block of code. Can anyone demonstrate this?
Do others have experience of this happening?
It seems he would prefer version 2. But I am wondering - if I cannot rely
on the error handling to be reset when I exit my function, can I guarantee
there is zero possibility of an error in the Exit_Handler part in version 2?
(e.g. the recordset wasn''t nothing, but trying to close it causes an error).
If there is an error in the Exit_Handler part, we obviously get stuck in a
never-ending loop, so to some extent it would make sense to make sure that
this cannot happen. The code is also less verbose, particularly when there
are many objects to be cleared up. Perhaps the answer is version 3 which
tacks on a final ''On Error GoTo 0'' but I have never seen anyone write a
function with that type of error handling.

**I am undecided and seeking the group''s opinions**

Public Function ContactExists1(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists1 = True
End If

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Public Function ContactExists2(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists2 = True
End If

Exit_Handler:

If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Public Function ContactExists3(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists3 = True
End If

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
On Error GoTo 0
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

推荐答案

就个人而言,我使用第二种变体。我猜关键是

你的错误处理程序没有创建新的错误。这条线; ''如果不是第一个

Nothing Then''被写入以便它不应该(很容易说在这种情况下不会是b $ b)错误。我认为,如果有一种方法可以编写代码

,这样你就可以消除错误,而不是忽略它们,这是优先选择的b / b
。请记住,错误处理程序的目的是使用
来处理错误(当你用这种方式说出来时似乎很明显:{))所以

问题是要么由处理程序解决,要么报告给用户

所以他可以向你报告(然后你修复问题和/或

错误处理程序来处理场景)。理想情况下,用户永远不会看到错误信息。现实地,错误将会发生,所以你需要以最友好的方式处理它们。


-

Bri


Anthony England写道:
Personally, I use the second variation. I guess the key thing is that
your Error handler not create new errors. The line; ''If Not rst Is
Nothing Then'' is written so that it should not (tempting to say will not
in this case) error. I think that if there is a way of writing the code
so that you eliminate errors rather than ignore them that that is
preferred. Keep in mind that the purpose of an error handler is to
handle the error (seems so obvious when you word it that way :{) ) so
that the problem is either solved by the handler or reported to the user
so he can report it to you (and then you fix the problem and/or the
error handler to deal with the scenario). Ideally, the user will never
see an error message. Realisticly, the errors will happen, so youneed to
handle them in the most user friendly way you can.

--
Bri

Anthony England wrote:
我正在考虑一般的错误处理例程,并编写了一个示例
函数来查找一个ID表。如果函数可以找到ID并根据该ID创建记录集,则该函数返回True,否则返回
false。

**我不是在寻找评论这个功能的用处 - 它只是为了演示错误处理**

这段代码有三个版本。大卫芬顿在早先的话题中说道:A97中的DAO特性?版本1是坏的,因为它有多个
行由On Error Resume Next覆盖,并且存在这种溢出到另一个代码块的危险。任何人都可以证明这一点吗?
其他人是否有过这种情况的经历?
看来他更喜欢第2版。但我想知道 - 如果我不能依赖
对错误处理进行重置我退出我的功能,我可以保证在版本2的Exit_Handler部分中没有错误的可能性吗?
(例如记录集不是什么,但试图关闭它会导致错误) 。
如果Exit_Handler部分出现错误,我们显然会陷入一个永无止境的循环,所以在某种程度上确保
不会发生这种情况是有意义的。代码也不那么冗长,特别是当有很多对象需要清理时。也许答案是版本3,它决定了On Error GoTo 0,但我从未见过有人用这种类型的错误处理来编写
函数。
**我尚未决定并寻求小组的意见**

公共功能ContactExists1(lngConID As Long)作为布尔

错误GoTo Err_Handler

Dim dbs作为DAO.Database
Dim rst as DAO.Recordset
Dim strSQL As String

strSQL =" SELECT tblContact。* FROM tblContact WHERE" &安培; _
ConID = &安培; CStr(lngConID)

设置dbs = CurrentDb

设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)

如果不是rst.EOF然后
ContactExists1 = True
结束如果

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
设置dbs = Nothing
退出函数

Err_Handler:
MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number
恢复Exit_Handler

结束功能

公共功能ContactExists2(lngConID As Long)作为布尔值

On Error GoTo Err_Handler

Dim dbs作为DAO.Database
Dim rst作为DAO.Recordset
Dim strSQL As String

strSQL =" SELECT tblContact。* FROM tblContact WHERE " &安培; _
ConID = &安培; CStr(lngConID)

设置dbs = CurrentDb

设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)

如果不是rst.EOF那么
ContactExists2 = True
结束如果

Exit_Handler:

如果不是第一次那么 rst.Close
设置rst = Nothing
结束如果

如果不是dbs什么都没有那么
设置dbs = Nothing
结束如果

退出功能
Err_Handler:
MsgBox Err.Description,vbExclamation,错误号: &安培; Err.Number
恢复Exit_Handler

结束功能

公共函数ContactExists3(lngConID As Long)作为布尔值

On Error GoTo Err_Handler

Dim dbs作为DAO.Database
Dim rst作为DAO.Recordset
Dim strSQL As String

strSQL =" SELECT tblContact。* FROM tblContact WHERE " &安培; _
ConID = &安培; CStr(lngConID)

设置dbs = CurrentDb

设置rst = dbs.OpenRecordset(strSQL,dbOpenForwardOnly,dbReadOnly)

如果不是rst.EOF那么
ContactExists3 = True
结束如果

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
设置dbs = Nothing
On Error GoTo 0
退出函数

Err_Handler:
MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number
恢复Exit_Handler

结束职能
I am considering general error handling routines and have written a sample
function to look up an ID in a table. The function returns True if it can
find the ID and create a recordset based on that ID, otherwise it returns
false.

**I am not looking for comments on the usefulness of this function - it is
only to demonstrate error handling**

There are three versions of this code. David Fenton says under the earlier
thread "DAO peculiarity in A97?" that version 1 is bad since it has multiple
lines covered by On Error Resume Next and that a danger exists of this
''spilling over'' into another block of code. Can anyone demonstrate this?
Do others have experience of this happening?
It seems he would prefer version 2. But I am wondering - if I cannot rely
on the error handling to be reset when I exit my function, can I guarantee
there is zero possibility of an error in the Exit_Handler part in version 2?
(e.g. the recordset wasn''t nothing, but trying to close it causes an error).
If there is an error in the Exit_Handler part, we obviously get stuck in a
never-ending loop, so to some extent it would make sense to make sure that
this cannot happen. The code is also less verbose, particularly when there
are many objects to be cleared up. Perhaps the answer is version 3 which
tacks on a final ''On Error GoTo 0'' but I have never seen anyone write a
function with that type of error handling.

**I am undecided and seeking the group''s opinions**

Public Function ContactExists1(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists1 = True
End If

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Public Function ContactExists2(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists2 = True
End If

Exit_Handler:

If Not rst Is Nothing Then
rst.Close
Set rst = Nothing
End If

If Not dbs Is Nothing Then
Set dbs = Nothing
End If

Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function

Public Function ContactExists3(lngConID As Long) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

strSQL = "SELECT tblContact.* FROM tblContact WHERE " & _
"ConID=" & CStr(lngConID)

Set dbs = CurrentDb

Set rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)

If Not rst.EOF Then
ContactExists3 = True
End If

Exit_Handler:
On Error Resume Next
rst.Close
Set rst = Nothing
Set dbs = Nothing
On Error GoTo 0
Exit Function

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function



Anthony England写道:
Anthony England wrote:
我正在考虑一般的错误处理例程,并编写了一个
示例函数来查找表中的ID。该函数返回
如果它可以找到ID并根据该ID创建记录集,则返回True。
否则返回false。

**我不是在寻找评论这个功能的用处 -
它只是为了演示错误处理**

这段代码有三个版本。大卫芬顿在早期的话题中说道:A97中的DAO特性?那个版本1很糟糕,因为它有多个线路被On Error Resume Next覆盖,并且存在这种'溢出'到另一个代码块中的危险。可以
任何人证明这一点?其他人是否有过这种情况的经历?
I am considering general error handling routines and have written a
sample function to look up an ID in a table. The function returns
True if it can find the ID and create a recordset based on that ID,
otherwise it returns false.

**I am not looking for comments on the usefulness of this function -
it is only to demonstrate error handling**

There are three versions of this code. David Fenton says under the
earlier thread "DAO peculiarity in A97?" that version 1 is bad since
it has multiple lines covered by On Error Resume Next and that a
danger exists of this ''spilling over'' into another block of code. Can
anyone demonstrate this? Do others have experience of this
happening?




仅限轶事,但我有数百个使用On Error的退出例程

在清理代码之前恢复并且从未见过

它的问题。


-

我不喜欢请查看此邮件附带的电子邮件帐户

。发送给... ...

在Hunter dot com的RBrandt



Anecdotal only, but I have hundreds of exit routines that use On Error
Resume next prior to the clean up code and have never seen an issue with
it.

--
I don''t check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com


2006年1月3日星期二21:33:21 GMT,Rick Brandt"

< ri ********* @ hotmail.com>写道:
On Tue, 03 Jan 2006 21:33:21 GMT, "Rick Brandt"
<ri*********@hotmail.com> wrote:
Anthony England写道:
Anthony England wrote:
我正在考虑一般错误处理例程并编写了一个
示例函数来查找一个ID表。该函数返回
如果它可以找到ID并根据该ID创建记录集,则返回True。
否则返回false。
**我不是在寻找关于此函数有用性的评论 -
只是为了演示错误处理**
此代码有三个版本。大卫芬顿在早期的话题中说道:A97中的DAO特性?那个版本1很糟糕,因为它有多个线路被On Error Resume Next覆盖,并且存在这种'溢出'到另一个代码块中的危险。可以
任何人证明这一点?其他人是否有过这种情况的经历?
I am considering general error handling routines and have written a
sample function to look up an ID in a table. The function returns
True if it can find the ID and create a recordset based on that ID,
otherwise it returns false.
**I am not looking for comments on the usefulness of this function -
it is only to demonstrate error handling**
There are three versions of this code. David Fenton says under the
earlier thread "DAO peculiarity in A97?" that version 1 is bad since
it has multiple lines covered by On Error Resume Next and that a
danger exists of this ''spilling over'' into another block of code. Can
anyone demonstrate this? Do others have experience of this
happening?


仅限轶事,但我有数百个使用On Error的退出例程
在清理代码之前恢复并且从未见过
它的一个问题。

Anecdotal only, but I have hundreds of exit routines that use On Error
Resume next prior to the clean up code and have never seen an issue with
it.




我自己也有同样的经历,Rick。


" On Error继续下一步是

Access(或更合适的是,VBA)开发人员工具箱中更强大的命令之一,但是

(已经说过)我还必须说它'无论出现什么错误,都不是万能的。检查错误集合仍然是* a

必须,因为使用On Error GoTo 0命令关闭

On Error Resume Next命令。

-

驱动器C:错误。 (A)bort(R)etry(S)mack The Darned Thing



I have the same experience myself, Rick.

"On Error Resume Next" is one of the more powerful commands in the
Access (or more appropriately, VBA) Developers tool box, however
(having said that) I must also say that it''s not a cure-all for
whatever errors appear. Checking the error collection(s) is *still* a
must, as is the use of the "On Error GoTo 0" command to turn off the
"On Error Resume Next" command.
--
Drive C: Error. (A)bort (R)etry (S)mack The Darned Thing


这篇关于David Fenton是否正确处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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