两种避免openFeatueclass发生错误的方法
利用workspace打开某个featureclass时,openFeatueclass方法有时会引发的错误。
这是因为找不到对应名字的featureclass, 而OpenFeatureClass方法自身又没有error handle.
一般情况下,我们可以在openfeatureclass前加上一句on error resume next以防止错误。
下面是一个利用On Error Resume Next避免错误的实例。
Public Function getFeatureClass(pFeatureWorkspace As IFeatureWorkspace, pFeatureClassName As String) As IFeatureClass
On Error Resume Next
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFeatureClassName)
If pFeatureClass Is Nothing Then
Call utlLogErr("Error in getFeatureClass : Cannot find featureclass " & pFeatureClassName & " ")
Set getFeatureClass = Nothing
Else
Set getFeatureClass = pFeatureClass
End If
End Function
On Error Resume Next
Dim pFeatureClass As IFeatureClass
Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFeatureClassName)
If pFeatureClass Is Nothing Then
Call utlLogErr("Error in getFeatureClass : Cannot find featureclass " & pFeatureClassName & " ")
Set getFeatureClass = Nothing
Else
Set getFeatureClass = pFeatureClass
End If
End Function
另一种避免openfeatureclass错误发生的方法,则是利用IWorkspace2.NameExists判断featureclass是否存在
Public Function getFeatureClass(pFeatureWorkspace As IFeatureWorkspace, pFeatureClassName As String) As IFeatureClass
Dim pWorkspace As IWorkspace2
Set pWorkspace = pFeatureWorkspace
If pWorkspace.NameExists(esriDTFeatureClass, pFeatureClassName) = True Then
Set getFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFeatureClassName)
Else
Call utlLogErr("Error in getFeatureClass : Cannot find featureclass " & pFeatureClassName & " ")
Set getFeatureClass = Nothing
End If
Set pWorkspace = Nothing
End Function
Dim pWorkspace As IWorkspace2
Set pWorkspace = pFeatureWorkspace
If pWorkspace.NameExists(esriDTFeatureClass, pFeatureClassName) = True Then
Set getFeatureClass = pFeatureWorkspace.OpenFeatureClass(pFeatureClassName)
Else
Call utlLogErr("Error in getFeatureClass : Cannot find featureclass " & pFeatureClassName & " ")
Set getFeatureClass = Nothing
End If
Set pWorkspace = Nothing
End Function
-----------------------------------------------------------
佛对我说:你心里有尘。我用力的拭擦。