VB.net 捕捉函数错误的逻辑
VB.net logic to catch errors in functions
我不是专业人士,我自己学习了一些编码规则。
我从 vba
开始,现在接近 vb.net
,但我的知识很差。
我编写这个函数是为了从记录集中提取列(在数据库上查询)并将其放入字符串的哈希集中。
尝试处理错误列数的错误,我使用了这些标准:
1) 在调用函数的代码中将“mErr
”声明为boolean
;
2) 发送“mErr
” byRef
到函数;
3) 发生错误时:将mErr
更改为true并在mHash
中插入一个空字符串。
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean)
Dim i As Long
Dim mHash As New HashSet(Of String)
If mRS.GetUpperBound(0) < mCol Then
mErr = True
mHash.Add("")
Return mHash
Exit Function
End If
For i = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, i))
Next
Return mHash
End Function
它似乎有效,但我认为这不是一个好的编码,我想提高我的编码技能。
我们将不胜感激。
您应该抛出并处理异常。看看 this.
您可以使用异常 来控制函数的错误。当您检测到函数中出现问题时,您将其抛出:
if ( something ) then 'check if something bad happened
Throw New Exception("X has occurred")
end if
然后,当这个函数抛出这个异常时,你应该像这样捕获它:
try
' Code with the call to the function which throws the exception
catch excp as Exception
'Your control code for this situation, as example just show it
MessageBox.show(excp.Message)
end
在您的代码中:
Exit Function
is unreachable since the Return
statement above the
Exit Function
function will transfer the control out to the
function.
More over a function should return a value otherwise you will get a
warning. in such cases you can use Sub
.
你的代码可以是
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean) As HashSet(Of String)
Dim loopCounter As Long
Dim mHash As New HashSet(Of String)
Try
If mRS.GetUpperBound(0) < mCol Then
Throw New Exception("")
End If
For loopCounter = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, loopCounter))
Next
Catch ex As Exception
mHash.Add(ex.ToString())
End Try
Return mHash
End Function
详细可以参考Try..Catch机制Click Here
有关 Exceptions
的更多信息
因为我注意到你的命名约定太差了,所以我建议你完成这个 article by Microsoft
在回答你的问题之前,让我先澄清两件事:
- 错误
- 异常
Exceptions are those which can be handled at the run time whereas
errors cannot be handled.
有效处理 exception/errors 始终是一个好习惯。
来了 Try-Catch-Throw 来拯救我们。
基本语法:
Try
' Do something in here that
' might raise an error.
Catch
' Handle exceptions that occur within
' the Try block, here.
Finally
' Perform cleanup code in here.
End Try
Try:Try 块标识将激活特定异常的代码块。其后跟一个或多个 Catch 块。
Catch:程序在程序中要处理问题的地方用异常处理程序捕获异常。 Catch关键字表示捕获异常。
Finally:Finally块用于执行一组给定的语句,无论是否抛出异常。例如,如果您打开一个文件,无论是否引发异常都必须关闭它。
Throw:程序出现问题时抛出异常。这是使用 Throw 关键字完成的。
如果您在程序中没有遇到任何异常,也建议使用 try catch 块
我不是专业人士,我自己学习了一些编码规则。
我从 vba
开始,现在接近 vb.net
,但我的知识很差。
我编写这个函数是为了从记录集中提取列(在数据库上查询)并将其放入字符串的哈希集中。
尝试处理错误列数的错误,我使用了这些标准:
1) 在调用函数的代码中将“mErr
”声明为boolean
;
2) 发送“mErr
” byRef
到函数;
3) 发生错误时:将mErr
更改为true并在mHash
中插入一个空字符串。
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean)
Dim i As Long
Dim mHash As New HashSet(Of String)
If mRS.GetUpperBound(0) < mCol Then
mErr = True
mHash.Add("")
Return mHash
Exit Function
End If
For i = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, i))
Next
Return mHash
End Function
它似乎有效,但我认为这不是一个好的编码,我想提高我的编码技能。
我们将不胜感激。
您应该抛出并处理异常。看看 this.
您可以使用异常 来控制函数的错误。当您检测到函数中出现问题时,您将其抛出:
if ( something ) then 'check if something bad happened
Throw New Exception("X has occurred")
end if
然后,当这个函数抛出这个异常时,你应该像这样捕获它:
try
' Code with the call to the function which throws the exception
catch excp as Exception
'Your control code for this situation, as example just show it
MessageBox.show(excp.Message)
end
在您的代码中:
Exit Function
is unreachable since theReturn
statement above theExit Function
function will transfer the control out to the function.More over a function should return a value otherwise you will get a warning. in such cases you can use
Sub
.
你的代码可以是
Public Function RSCol(ByVal mRS As Object, ByVal mCol As Byte, ByRef mErr As Boolean) As HashSet(Of String)
Dim loopCounter As Long
Dim mHash As New HashSet(Of String)
Try
If mRS.GetUpperBound(0) < mCol Then
Throw New Exception("")
End If
For loopCounter = 0 To mRS.GetUpperBound(1)
mHash.Add(mRS(mCol, loopCounter))
Next
Catch ex As Exception
mHash.Add(ex.ToString())
End Try
Return mHash
End Function
详细可以参考Try..Catch机制Click Here 有关 Exceptions
的更多信息因为我注意到你的命名约定太差了,所以我建议你完成这个 article by Microsoft
在回答你的问题之前,让我先澄清两件事:
- 错误
- 异常
Exceptions are those which can be handled at the run time whereas errors cannot be handled.
有效处理 exception/errors 始终是一个好习惯。
来了 Try-Catch-Throw 来拯救我们。
基本语法:
Try
' Do something in here that
' might raise an error.
Catch
' Handle exceptions that occur within
' the Try block, here.
Finally
' Perform cleanup code in here.
End Try
Try:Try 块标识将激活特定异常的代码块。其后跟一个或多个 Catch 块。
Catch:程序在程序中要处理问题的地方用异常处理程序捕获异常。 Catch关键字表示捕获异常。
Finally:Finally块用于执行一组给定的语句,无论是否抛出异常。例如,如果您打开一个文件,无论是否引发异常都必须关闭它。
Throw:程序出现问题时抛出异常。这是使用 Throw 关键字完成的。
如果您在程序中没有遇到任何异常,也建议使用 try catch 块