gpt4 book ai didi

.net - 尝试...捕获...最后在VB.NET中

转载 作者:行者123 更新时间:2023-12-04 06:57:00 26 4
gpt4 key购买 nike

在VB.NET中,如果您从Catch返回一个值,那么Finally代码是否仍将执行?

例如(我对此代码进行了概括):

Try
response = Client.doRequest()
Catch ex As Exception
'Request threw an error - Fatal failure.
InsertErrorLog(ex)
Return False
Finally
DisposeClient()
End Try

我需要确保 DisposeClient()一直执行。因为我要退出 Catch,仍然会执行 Finally吗?

最佳答案

不管是否执行 Finally 块,无论执行什么代码,始终执行 Catch 块。

引用:https://msdn.microsoft.com/en-us/library/fk6t46tz.aspx

使用以下代码尝试一下:

Dim Temp As String

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Temp = "A"

MessageBox.Show(Test())
MessageBox.Show(Temp)
End Sub

Private Function Test() As String
Try
Temp = "B"
Throw New Exception()
Temp = "C"

Return "Try"
Catch ex As Exception
Temp = "D"
Return "Catch"
Finally
Temp = "E"
End Try

Temp = "F"
Return "End"
End Function

它显示消息:
Catch

然后
E

这意味着,即使函数确实在 Finally 块中返回,也始终执行 Catch 块。

关于.net - 尝试...捕获...最后在VB.NET中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com