gpt4 book ai didi

vb.net - 退出尝试与退出子

转载 作者:行者123 更新时间:2023-12-02 08:19:14 24 4
gpt4 key购买 nike

我有以下 Try 语句,尽管当写入数据库的值太大时,此后的所有内容都不会运行。我应该使用 Exit Try 而不是 Exit Sub 吗?

Try

Dim conn As OracleConnection
Dim cmdProduction As New OracleCommand
conn = GetConnect()
conn.Open()
cmdProduction.Connection = conn
cmdProduction.CommandText = "INSERT INTO PRODUCTION (DateTime, Plant, Runhours, Tons, Shift, Sft, Plant_Ord) Values ('" & DateTime & "','FM1-Selox','" & (seloxRuntime / 60) & "','" & seloxTons & "','" & theShift & "','" & sft & "','51')"
cmdProduction.ExecuteNonQuery()
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
conn.Close()
Exit Sub
Finally
conn.Close()
End Try

最佳答案

您也不需要,从 Catch 中删除 conn.CloseExit Sub 就可以了。 conn.Close将从Finally处执行。

Try
'
'
'
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
Finally
conn.Close()
End Try

当然,如果您的示例代码不完整并且实际上如下所示:

Finally
conn.Close()
End Try

' More code

然后使用Exit Sub,或将剩余代码移至Try

Try
'
'
'
' More code
Catch ex As Exception
WriteToFile("Production - Production : Database Error : " & ex.Message)
Finally
conn.Close()
End Try

关于vb.net - 退出尝试与退出子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29407674/

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