本文介绍了David Fenton是否正确处理错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在考虑一般错误处理例程,并编写了一个示例 函数来查找表中的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 samplefunction to look up an ID in a table. The function returns True if it canfind the ID and create a recordset based on that ID, otherwise it returnsfalse.**I am not looking for comments on the usefulness of this function - it isonly to demonstrate error handling**There are three versions of this code. David Fenton says under the earlierthread "DAO peculiarity in A97?" that version 1 is bad since it has multiplelines 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 relyon the error handling to be reset when I exit my function, can I guaranteethere 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 anever-ending loop, so to some extent it would make sense to make sure thatthis cannot happen. The code is also less verbose, particularly when thereare many objects to be cleared up. Perhaps the answer is version 3 whichtacks on a final ''On Error GoTo 0'' but I have never seen anyone write afunction with that type of error handling.**I am undecided and seeking the group''s opinions**Public Function ContactExists1(lngConID As Long) As BooleanOn Error GoTo Err_HandlerDim dbs As DAO.DatabaseDim rst As DAO.RecordsetDim strSQL As StringstrSQL = "SELECT tblContact.* FROM tblContact WHERE " & _"ConID=" & CStr(lngConID)Set dbs = CurrentDbSet rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)If Not rst.EOF ThenContactExists1 = TrueEnd IfExit_Handler:On Error Resume Nextrst.CloseSet rst = NothingSet dbs = NothingExit FunctionErr_Handler:MsgBox Err.Description, vbExclamation, "Error No: " & Err.NumberResume Exit_HandlerEnd FunctionPublic Function ContactExists2(lngConID As Long) As BooleanOn Error GoTo Err_HandlerDim dbs As DAO.DatabaseDim rst As DAO.RecordsetDim strSQL As StringstrSQL = "SELECT tblContact.* FROM tblContact WHERE " & _"ConID=" & CStr(lngConID)Set dbs = CurrentDbSet rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)If Not rst.EOF ThenContactExists2 = TrueEnd IfExit_Handler:If Not rst Is Nothing Thenrst.CloseSet rst = NothingEnd IfIf Not dbs Is Nothing ThenSet dbs = NothingEnd IfExit FunctionErr_Handler:MsgBox Err.Description, vbExclamation, "Error No: " & Err.NumberResume Exit_HandlerEnd FunctionPublic Function ContactExists3(lngConID As Long) As BooleanOn Error GoTo Err_HandlerDim dbs As DAO.DatabaseDim rst As DAO.RecordsetDim strSQL As StringstrSQL = "SELECT tblContact.* FROM tblContact WHERE " & _"ConID=" & CStr(lngConID)Set dbs = CurrentDbSet rst = dbs.OpenRecordset(strSQL, dbOpenForwardOnly, dbReadOnly)If Not rst.EOF ThenContactExists3 = TrueEnd IfExit_Handler:On Error Resume Nextrst.CloseSet rst = NothingSet dbs = NothingOn Error GoTo 0Exit FunctionErr_Handler:MsgBox Err.Description, vbExclamation, "Error No: " & Err.NumberResume Exit_HandlerEnd Function推荐答案 仅限轶事,但我有数百个使用On Error的退出例程 在清理代码之前恢复并且从未见过 它的问题。 - 我不喜欢请查看此邮件附带的电子邮件帐户 。发送给... ... 在Hunter dot com的RBrandtAnecdotal only, but I have hundreds of exit routines that use On ErrorResume next prior to the clean up code and have never seen an issue withit.--I don''t check the Email account attachedto this message. Send instead to...RBrandt at Hunter dot com 仅限轶事,但我有数百个使用On Error的退出例程在清理代码之前恢复并且从未见过它的一个问题。Anecdotal only, but I have hundreds of exit routines that use On ErrorResume next prior to the clean up code and have never seen an issue withit. 我自己也有同样的经历,Rick。 " On Error继续下一步是 Access(或更合适的是,VBA)开发人员工具箱中更强大的命令之一,但是 (已经说过)我还必须说它'无论出现什么错误,都不是万能的。检查错误集合仍然是* a 必须,因为使用On Error GoTo 0命令关闭 On Error Resume Next命令。 - 驱动器C:错误。 (A)bort(R)etry(S)mack The Darned ThingI have the same experience myself, Rick."On Error Resume Next" is one of the more powerful commands in theAccess (or more appropriately, VBA) Developers tool box, however(having said that) I must also say that it''s not a cure-all forwhatever errors appear. Checking the error collection(s) is *still* amust, 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是否正确处理错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 19:58
查看更多